Domanda

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.

È stato utile?

Soluzione

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
)

Altri suggerimenti

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

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.

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top