5

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.

sll
  • 61,540
  • 22
  • 104
  • 156
user1029660
  • 53
  • 1
  • 5

4 Answers4

8

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
)
Nikola Radosavljević
  • 6,871
  • 32
  • 44
1

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. :)

Chris
  • 27,210
  • 6
  • 71
  • 92
  • no; this is not a job for Encoding; Encoding is string => byte[] => string; this question is byte[] => string => byte[] - base-64 or hex-encoding is correct; Encoding is wrong. – Marc Gravell Nov 04 '11 at 12:39
  • @MarcGravell: Yeah, my bad. I didn't read the question. I thought he had a text string as bytes since he was using encoding. It was only afterwards I realised that some of the characters would be non-printing and then saw your comment and read the question properly. That -1 will make me remember to read the question properly in future. ;-) – Chris Nov 04 '11 at 12:43
0

Use a different Encoding base as ASCII will change all non-printable chars to ? being 63.

When the string doesn't have to be a human readable version of the non-prontable chars indeed converting it to base64 (UUEncode/XXEncode) would indeed do the trick.

NitWit
  • 152
  • 5
  • no; this is not a job for Encoding; Encoding is string => byte[] => string; this question is byte[] => string => byte[] - base-64 or hex-encoding is correct; Encoding is wrong. – Marc Gravell Nov 04 '11 at 12:39
0

Your using the ASCI encoding to convery your byte array to string, remember that ASCI is a 7 bit protocol, the encoding will either strip the eigth bit or fall back to a specific value (the documentation seems unclear which it does!)

To quote MSDN;

Prior to the .NET Framework version 2.0, the .NET Framework allowed spoofing by ignoring the 8th bit. Beginning with the .NET Framework 2.0, non-ASCII code points fall back during decoding.

Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62