Binary to string conversion

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
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top