I would like to convert a byte array containing non printable characters to string for my application. When I convert back to byte array, the contents of the array should remain the same as I found that ASCII/Unicode/UTF8 doesnt always give me the right solution?
E.g
byte[] bytearray ={ 147, 35, 44, 18, 255, 104, 206, 72 ,69};
string str = System.Text.Encoding.ASCII.GetString(bytearray);
bytearray = System.Text.Encoding.ASCII.GetBytes(str);
In the above example, I find that the byte array contains
{ 63, 35, 44, 18, 63, 104, 63, 72 ,69}.
Kindly help me.
You can download Notepad++ and open the file there. Then, go to the menu and select View->Show Symbol->Show All Characters . All characters will become visible, but you will have to scroll through the whole file to see which character needs to be removed.
Some of the most common non printable characters are carriage return, form feed, line feed, backspace, escape, horizontal tab and vertical tab. These might not have a visible shape but will have effects on the output. To further understand them, we have to look into ASCII table.
Non-printing characters or formatting marks are characters for content designing in word processors, which are not displayed at printing. It is also possible to customize their display on the monitor. The most common non-printable characters in word processors are pilcrow, space, non-breaking space, tab character etc.
Non-printable characters are those that are not visible and do not take up any printing space. Some Unicode characters, such as “Other” and “Separator,” are not printable. Non-Printable characters include all escape characters such as '\n', \'t', '\r', '\x16', '\xlf', and so on.
Take a look at Convert.ToBase64String method. It will convert a byte array into string. Have in mind that encoded into string that data will take up more space than your original byte array would.
public static string ToBase64String(
byte[] inArray
)
You can then decode string back to byte array using FromBase64String
public static byte[] FromBase64String(
string s
)
I think that your problem is that you are using the wrong encoding. ASCII defines 128 characters (http://en.wikipedia.org/wiki/ASCII) and so will never give you bytes above 128.
You need to find your correct encoding and use that if you expect a return trip to be successful.
I misread the question it seems. My answer was only relevant if the byte array was an encoded string - I hadn't read the bit that said that it was unprintable characters, etc. Nikola's answer is the one to go for. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With