I use the code below generate a XML encoding of string str:
我使用下面的代码生成字符串str的XML编码:
str := string([]byte{0x01})
marshalBytes, _ := xml.Marshal(str)
fmt.Println(string(marshalBytes)) // output: <string>�</string>; � is [239 191 189] in bytes.
Obviously, � is not equivalent of 0x01.
显然,不相当于0 x01�。
How can I fix it?
我怎样才能修好它呢?
1 个解决方案
#1
5
The bytes [239 191 189] are the UTF-8 encoding of the Unicode Replacement Character.
字节[239 191 189]是Unicode替换字符的UTF-8编码。
The XML marshaler replaces the byte 0x1 with the Unicode Replacement Character because the byte 0x01 is not a legal character in XML.
XML封送器用Unicode替换字符替换字节0x1,因为字节0x01在XML中不是合法字符。
It is not possible to prevent the XML marshaler from using the replacement.
阻止XML封送器使用替换是不可能的。
#1
5
The bytes [239 191 189] are the UTF-8 encoding of the Unicode Replacement Character.
字节[239 191 189]是Unicode替换字符的UTF-8编码。
The XML marshaler replaces the byte 0x1 with the Unicode Replacement Character because the byte 0x01 is not a legal character in XML.
XML封送器用Unicode替换字符替换字节0x1,因为字节0x01在XML中不是合法字符。
It is not possible to prevent the XML marshaler from using the replacement.
阻止XML封送器使用替换是不可能的。