简体   繁体   中英

Converting Bytes to String results in ? rather than £ (ASCII)

I have pulled a predefined message from a network stream and I convert it to a string using:

string payloadString = System.Text.Encoding.ASCII.GetString(payloadBytes);

I have noticed from reading here: http://www.ascii-code.com/

that £ is only for ASCII extended (127+) and the one I am using above only covers non extended ASCII (0 - 127).

This results in some of my payload containing '?' rather than '£'.

Is there a way of converting the bytes using ASCII Extended encoding?

Thanks.

Use Encoding.Default (which is the system's default ANSI code page) or Encoding.UTF8 , rather than Encoding.ASCII . In any case, check with whoever sends the message what the encoding is supposed to be, and use that.

使用UTF8编码对象将Unicode字符编码为每个字符一到四个字节的序列,这将确保字符的正确编码

string payloadString = System.Text.Encoding.UTF8.GetString(payloadBytes);

更改您的代码:

string payloadString = System.Text.Encoding.UTF8.GetString(payloadBytes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM