Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert byte array -> string -> byte array corrupts data

Can anyone tell me what is going on here?

        byte[] stamp = new byte[]{0,0,0,0,0,1,177,115};
        string serialize = System.Text.Encoding.UTF8.GetString(stamp);
        byte[] deserialize = System.Text.Encoding.UTF8.GetBytes(serialize);

        //deserialize == byte[]{0,0,0,0,0,1,239,191,189,115}

Why is stamp != deserialize??

like image 364
sǝɯɐſ Avatar asked Jan 13 '23 20:01

sǝɯɐſ


1 Answers

In your original byte array, you have the 177 character, which is the plusminus sign. However during the serialization, that code isn't being recognized. It's being replaced by 239 191 189 which is the REPLACEMENT CHARACTER.

Here's a chart for reference. http://www.utf8-chartable.de/unicode-utf8-table.pl?start=65280&utf8=dec

I'm not quite sure WHY the plusminus sign isn't recognized, but that's why the byte arrays aren't equal. Other than that swap, they would be equal and the data isn't corrupted in any way.

like image 172
Eric Wich Avatar answered Jan 24 '23 03:01

Eric Wich