Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Encoding.UTF8.GetString解析错误?_C#_Sockets_Utf 8_Tcp_Unity3d - Fatal编程技术网

C# Encoding.UTF8.GetString解析错误?

C# Encoding.UTF8.GetString解析错误?,c#,sockets,utf-8,tcp,unity3d,C#,Sockets,Utf 8,Tcp,Unity3d,为什么最后12个字节没有被解析,以及Encoding.UTF8.GetString如何处理UTF-8字节,我的意思是它如何判断这是一个汉字还是一个ascii字符正在被解析。但结果字符串中嵌入了空值,因此无法正确打印/显示 试试这个: // test code void Start() { // bytes is get from server with NetworkStream.Read() byte[] bytes = { 0xe9, 0xba, 0xbb, // 麻

为什么最后12个字节没有被解析,以及
Encoding.UTF8.GetString
如何处理UTF-8字节,我的意思是它如何判断这是一个汉字还是一个ascii字符

正在被解析。但结果字符串中嵌入了空值,因此无法正确打印/显示

试试这个:

// test code
void Start()
{
    // bytes is get from server with NetworkStream.Read()
    byte[] bytes = { 0xe9, 0xba, 0xbb, // 麻
                     0xe9, 0xa3, 0x8e, // 风
                     0xe4, 0xbe, 0x8f, // 侏
                     0xe5, 0x84, 0x92, // 儒
                     0x00, 0x00, 0x00, 
                     0x00, 0x00, 0x31, 
                     0x32, 0x00, 0x00, 
                     0x00, 0x00, 0x00 };
    // print: 麻风侏儒
    Debug.Log(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
}
MyChars拥有一切

String MyString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

char[] MyChars = MyString.ToArray();