c# and Encoding.ASCII.GetString

后端 未结 5 1900
南旧
南旧 2020-12-06 03:51
byte[] header = new byte[]{255, 216}; 

string ascii =  Encoding.ASCII.GetString(header);

I expect ASCII to be equal to be FFD8 (JPEG SOI marker)<

5条回答
  •  一个人的身影
    2020-12-06 04:29

    In this case you'd be better to compare the byte arrays rather than converting to string.

    If you must convert to string, I suggest using the encoding Latin-1 aka ISO-8859-1 aka Code Page 28591 encoding, as this encoding will map all bytes with hex values are in the range 0-255 to the Unicode character with the same hex value - convenient for this scenario. Any of the following will get this encoding:

    Encoding.GetEncoding(28591)
    Encoding.GetEncoding("Latin1")
    Encoding.GetEncoding("ISO-8859-1")
    

提交回复
热议问题