Binary to string conversion

  • Thread starter Thread starter Diablo
  • Start date Start date
D

Diablo

Hi,

I am converting a byte array to string using
System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the
resulting text is converted back to binary, using the reverse function:
System.Text.Encoding.UTF8.GetBytes() or Unicode.GetBytes.
But the result does not match the original byte array!
Is the conversion loosing some characters? Why?

Thanks,
Diablo
 
Diablo said:
I am converting a byte array to string using
System.Text.Encoding.UTF8.GetString() or Unicode.GetString(). Then the
resulting text is converted back to binary, using the reverse function:
System.Text.Encoding.UTF8.GetBytes() or Unicode.GetBytes.
But the result does not match the original byte array!
Is the conversion loosing some characters? Why?

Presumably because the original binary data isn't a valid UTF-8 byte
sequence.

You shouldn't use encodings like this to convert arbitrary binary data
into text - use Convert.ToBase64String instead.
 
Back
Top