Go xml.Marshal返回无效字符

时间:2014-10-16 12:47:17

标签: xml encoding go

我使用下面的代码生成字符串str的XML编码:

str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>�</string>; � is [239 191 189] in bytes.

显然, 不等于0x01。

我该如何解决?

1 个答案:

答案 0 :(得分:5)

字节[239 191 189]是Unicode Replacement Character的UTF-8编码。

XML封送程序将字节0x1替换为Unicode替换字符,因为 字节0x01不是legal character in XML

无法阻止XML封送程序使用替换。

相关问题