1

I use the code below generate a XML encoding of string str:

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.

How can I fix it?

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
liudanking
  • 121
  • 1
  • 7

1 Answers1

5

The bytes [239 191 189] are the UTF-8 encoding of the Unicode Replacement Character.

The XML marshaler replaces the byte 0x1 with the Unicode Replacement Character because the byte 0x01 is not a legal character in XML.

It is not possible to prevent the XML marshaler from using the replacement.

Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242