Sending a string containing special characters through a TcpClient (byte[])

后端 未结 3 1014
轻奢々
轻奢々 2021-01-05 01:54

I\'m trying to send a string containing special characters through a TcpClient (byte[]). Here\'s an example:

  • Client enters \"amé\" in a textbox
  • Client
3条回答
  •  孤城傲影
    2021-01-05 02:24

    Your problem appears to be the Encoding.ASCII.GetBytes("amé"); and Encoding.ASCII.GetString(buffer); calls, as hinted at by '500 - Internal Server Error' in his comments.

    The é character is a multi-byte character which is encoded in UTF-8 with the byte sequence C3 A9. When you use the Encoding.ASCII class to encode and decode, the é character is converted to a question mark since it does not have a direct ASCII encoding. This is true of any character that has no direct coding in ASCII.

    Change your code to use Encoding.UTF8.GetBytes() and Encoding.UTF8.GetString() and it should work for you.

提交回复
热议问题