我使用下面的代码生成字符串 str 的 XML 编码:
str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>�</string>; � is [239 191 189] in bytes.
显然,� 不等于 0x01。
我该如何解决?
最佳答案
字节 [239 191 189] 是 Unicode Replacement Character 的 UTF-8 编码.
XML 编码器将字节 0x1 替换为 Unicode 替换字符,因为 字节 0x01 不是 legal character in XML .
不可能阻止 XML 编码器使用替换。
关于xml - Go xml.Marshal 返回无效字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26404864/