Go

时间:2019-01-30 12:47:35

标签: c# go utf-8

Go中C#的Encoding.UTF8.GetString等效于什么?

我已经知道Go的默认编码是UTF8,而Go中的字符串(某字节)会产生UTF8编码的字符串。

C#:

public static void Main()
{
    byte[] bytes = new byte[] { 144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239};
    string str = Encoding.UTF8.GetString(bytes);

    Console.WriteLine(str);
 }

开始:

func main() {
bytes := []byte { 144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239}
str := string(bytes)
fmt.Println(str)
}

C#代码产生:

�������*\��W��P�p��

Go代码产生:

�������*\��W���P�p��

我在这里想念什么?

1 个答案:

答案 0 :(得分:2)

很明显,无论您以何种方式看待,您的bytes都不是有效的UTF-8。

例如,

package main

import (
    "fmt"
)

func main() {
    bytes := []byte{144, 197, 217, 192, 204, 249, 181, 42, 92, 252, 243, 87, 170, 243, 169, 80, 175, 112, 192, 239}
    fmt.Println(len(bytes))
    fmt.Printf("%v\n", bytes)
    fmt.Printf("% x\n", bytes)
    fmt.Printf("%q\n", bytes)
    fmt.Printf("%s\n", bytes)
}

游乐场:https://play.golang.org/p/bHhkeGuZcCK

输出:

20
[144 197 217 192 204 249 181 42 92 252 243 87 170 243 169 80 175 112 192 239]
90 c5 d9 c0 cc f9 b5 2a 5c fc f3 57 aa f3 a9 50 af 70 c0 ef
"\x90\xc5\xd9\xc0\xcc\xf9\xb5*\\\xfc\xf3W\xaa\xf3\xa9P\xafp\xc0\xef"
�������*\��W���P�p��

参考文献:

The Unicode Consortium

Unicode: UTF-8, UTF-16, UTF-32 & BOM

UTF-8 - Wikipedia

The Go Blog: Strings, bytes, runes and characters in Go

Go: Package utf8