Print a string in Hex representation or print control characters as literals
<p>I have an application which uses clipboard. Users copy data from excel and paste it into my application. </p>

<p>However, sometimes my application cannot handle the data correctly (in production). But I cannot reproduce the problem. Because the spreadsheet user is using contains his own macro and I am not allowed to have his original spreadsheet, I am thinking to add code to dump the text in the clipboard completely to a log file, so that I can see if the data being pasted is in the correct format.</p>

<p>I am especially concerned with the control characters contained in the clipboard. Therefore I want to dump the string as literal. For example, if string in the clipboard contains <code>'\t'</code>, I want to see <code>"\t"</code> in the log file, instead of a tab. Is there a way to do this? </p>

<p>Alternatively I can log the text in the hex format. But it seems I need to first convert the <code>string</code> to a <code>char[]</code> and use <code>System.Buffer.BlockCopy</code> to copy the <code>char[]</code> to a <code>byte[]</code>. And then print the byte[] using <code>BitConverter.ToString()</code>. It works fine. But is there a better way (easier, no need to copy, no need to loop through every character) to do this? .Net does not have a build-in function for this?</p>

<p>I used this as reference: <a href="http://stackoverflow.com/questions/623104/byte-to-hex-string/5919521#5919521">byte[] to hex string</a></p>

<p>Methods suggested there look a little bit "heavy".</p>
