0

I have an application which uses clipboard. Users copy data from excel and paste it into my application.

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.

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 '\t', I want to see "\t" in the log file, instead of a tab. Is there a way to do this?

Alternatively I can log the text in the hex format. But it seems I need to first convert the string to a char[] and use System.Buffer.BlockCopy to copy the char[] to a byte[]. And then print the byte[] using BitConverter.ToString(). 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?

I used this as reference: byte[] to hex string

Methods suggested there look a little bit "heavy".

Community
  • 1
  • 1
Xinchao
  • 2,929
  • 1
  • 24
  • 39
  • Hm, you could do a "find & replace" with the control characters - simply search the string for them and replace them with something that fits your purpose. – Christian Sauer May 02 '13 at 07:34

1 Answers1

1

Simple Try this

"some text \t some text".Replace("\t",@"\t");
Civa
  • 2,058
  • 2
  • 18
  • 30
  • Thanks Civa and Christian Sauer. The reason I prefer not to do it this way is I dont know if all control characters (or all invisible characters) can be represented by a single symbol. I dont want to enumerate every control character manually, and replace them manually. What I am looking for is: if mystring contains '\something', I replace it with @"\something". Is it possible? – Xinchao May 02 '13 at 09:32
  • you can took reference control characters http://en.wikipedia.org/wiki/C0_and_C1_control_codes – Civa May 02 '13 at 13:11