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?

Bayta Darell
  • 101,448
  • 10
  • 207
  • 215
liudanking
  • 101
  • 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.

Bayta Darell
  • 101,448
  • 10
  • 207
  • 215