views:

279

answers:

3

I have a binary array. In the process of conversion it into string,due to some data my string gets terminated. and it ignores next data.Have a look on my code. Is there is any mistake??

str += (char)chunkData[index].ToString();

Later on i want to display it on textbox. My array contains following data as display in hex editor.

 xÚb```e``*bxÚb`
    ¨€ˆY8ÄX¡˜A‰“yuZs˜#µjтЬi@š
    È4„è0

I tried this but nothing change.. str = System.Text.Encoding.ASCII.GetString(chunkData);

+3  A: 

See Encoding.GetString with the encoding matching your binary data.

str = System.Text.Encoding.ASCII.GetString(chunkData);
Guillaume
A: 

Your "mistake" is not using the Encoding.GetString(byte[]) call for that :-)

Thorsten Dittmar
Getting downvoted for what?
Thorsten Dittmar
+2  A: 

You shouldn't use text to represent arbitrary binary data. You will almost certainly lose data if you just use Encoding.GetString and Encoding.GetBytes.

If you really want to convert arbitrary binary data to text and back, use Convert.ToBase64String and Convert.FromBase64String.

As for '\0' termination: .NET strings themselves don't rely on termination characters, but many UI controls (including TextBox) will treat '\0' as a termination character.

Jon Skeet
About \0 termination: Really? I didn't know that - how is the length of a .NET string being determined?
Thorsten Dittmar
+1 for Convert.ToBase64String. I think thats the correct way to show arbitrary data in TextBox or any control. User will be unable to interpret it anyway. Regarding the termination char, I would like to text the '\0' thing with a textbox.
A9S6
@Thorsten: The string object has the length as a separate variable.
Jon Skeet