C# 通过 TcpClient (byte[]) 发送包含特殊字符的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15082285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:59:31  来源:igfitidea点击:

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

c#.netencodingtcpspecial-characters

提问by Philippe Paré

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

我正在尝试通过 TcpClient (byte[]) 发送一个包含特殊字符的字符串。下面是一个例子:

  • Client enters "amé" in a textbox
  • Client converts string to byte[] using a certain encoding (I've tried all the predefined ones plus some like "iso-8859-1")
  • Client sends byte[] through TCP
  • Server receives and outputs the string reconverted with the same encoding (to a listbox)
  • 客户在文本框中输入“amé”
  • 客户端使用某种编码将字符串转换为字节 [] (我已经尝试了所有预定义的编码以及诸如“iso-8859-1”之类的编码)
  • 客户端通过 TCP 发送 byte[]
  • 服务器接收并输出使用相同编码重新转换的字符串(到列表框)

Edit :

编辑 :

I forgot to mention that the resulting string was "am?".

我忘了提到结果字符串是“am?”。

Edit-2 (as requested, here's some code):

Edit-2(根据要求,这里有一些代码):

@DJKRAZE here's a bit of code :

@DJKRAZE 这里有一些代码:

byte[] buffer = Encoding.ASCII.GetBytes("amé");
(TcpClient)server.Client.Send(buffer);

On the server side:

在服务器端:

byte[] buffer = new byte[1024];
Client.Recieve(buffer);
string message = Encoding.ASCII.GetString(buffer);
ListBox1.Items.Add(message);

The string that appears in the listbox is "am?"

列表框中出现的字符串是“am?”

=== Solution ===

=== 解决方案 ===

Encoding encoding = Encoding.GetEncoding("iso-8859-1");
byte[] message = encoding.GetBytes("babé");

Update:

更新:

Simply using Encoding.Utf8.GetBytes("ééé");works like a charm.

简单地使用Encoding.Utf8.GetBytes("ééé");作品就像一个魅力。

采纳答案by Philippe Paré

Never too late to answer a question I think, hope someone will find answers here.

回答我认为的问题永远不会太晚,希望有人能在这里找到答案。

C# uses 16 bit chars, and ASCII truncates them to 8 bit, to fit in a byte. After some research, I found UTF-8 to be the best encoding for special characters.

C# 使用 16 位字符,ASCII 将它们截断为 8 位,以适应一个字节。经过一番研究,我发现 UTF-8 是特殊字符的最佳编码。

//data to send via TCP or any stream/file
byte[] string_to_send = UTF8Encoding.UTF8.GetBytes("amé");

//when receiving, pass the array in this to get the string back
string received_string = UTF8Encoding.UTF8.GetString(message_to_send);

回答by Mohsen Heydari

Your question and your error is not clear to me but using Base64Stringmay solve the problem
Something like this

你的问题,你的错误我也不清楚,但使用Base64String可以解决这个问题
大概是这样

static public string EncodeTo64(string toEncode)
    {
      byte[] toEncodeAsBytes
            = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
      string returnValue
            = System.Convert.ToBase64String(toEncodeAsBytes);
      return returnValue;
    }

static public string DecodeFrom64(string encodedData)
    {
      byte[] encodedDataAsBytes
          = System.Convert.FromBase64String(encodedData);
      string returnValue =
         System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
      return returnValue;
    }

回答by Corey

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.

您的问题似乎是Encoding.ASCII.GetBytes("amé");Encoding.ASCII.GetString(buffer);调用,正如他的评论中的“500 - 内部服务器错误”所暗示的那样。

The écharacter is a multi-byte character which is encoded in UTF-8 with the byte sequence C3 A9. When you use the Encoding.ASCIIclass 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.

é字符是一个多字节字符,以 UTF-8 编码,字节序列为C3 A9。当您使用Encoding.ASCII该类进行编码和解码时,该é字符将转换为问号,因为它没有直接的 ASCII 编码。这适用于任何没有直接用 ASCII 编码的字符。

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

修改代码以使用Encoding.UTF8.GetBytes()Encoding.UTF8.GetString()它应该为你工作。