elle0087
elle0087

Reputation: 911

Coding and decoding byte array in string are not identical

I have to read a varbinary column from SQL Server, and send it to a json.

I tried some test conversions byte[]/string and vice-versa, but I cannot understand what is wrong.

For example:

string s = System.Text.Encoding.Unicode.GetString((byte[])value);
byte[] ba = Encoding.Unicode.GetBytes(s);
bool f = ba.SequenceEqual((byte[])value); //false??? why??

Upvotes: -2

Views: 55

Answers (1)

elle0087
elle0087

Reputation: 911

as @Jon Skeet suggests:

string s = Convert.ToBase64String((byte[])value);
byte[] ba = Convert.FromBase64String(s);
bool f = ba.SequenceEqual((byte[])value); //true!!

Upvotes: 0

Related Questions