我正在尝试通过 TcpClient (byte[]) 发送包含特殊字符的字符串。这是一个例子:
- 客户在文本框中输入“amé”
- 客户端使用某种编码将字符串转换为字节[] (我已经尝试了所有预定义的以及一些像“iso-8859-1”这样的)
- 客户端通过 TCP 发送 byte[]
- 服务器接收并输出使用相同编码重新转换的字符串(到列表框)
编辑 :
我忘了提到结果字符串是“am?”。
Edit-2(根据要求,这里有一些代码):
@DJKRAZE 这里有一些代码:
byte[] buffer = Encoding.ASCII.GetBytes("amé");
(TcpClient)server.Client.Send(buffer);
在服务器端:
byte[] buffer = new byte[1024];
Client.Recieve(buffer);
string message = Encoding.ASCII.GetString(buffer);
ListBox1.Items.Add(message);
出现在列表框中的字符串是“am?”
=== 解决方案 ===
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
byte[] message = encoding.GetBytes("babé");
更新:
简单地使用Encoding.Utf8.GetBytes("ééé");
就像魅力一样。