When I add a single byte to my string at 0x80 or above, golang will add 0xc2 before my byte. I think this has something to do with utf8 runes. Either way, how do I just add 0x80 to the end of my string?
Example:
var s string = ""
len(s) // this will be 0
s += string(0x80)
len(s) // this will be 2, string is now bytes 0xc2 0x80
The From the specification:
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
The expression string(0x80) evaluates to a string with the UTF-8 representation of 0x80, not a string containing the single byte 0x80. The UTF-8 representation of 0x80 is 0xc2 0x80.
Use the \x hex escape to specify the byte 0x80 in a string:
s += "\x80"
You can create a string from an arbitrary sequence of bytes using the string([]byte) conversion.
s += string([]byte{0x80})
I haven't found a way to avoid adding that character, if I use string(0x80)
to convert the byte. However, I did find that if I change the whole string to a slice of bytes, then add the byte, then switch back to a string, I can get the correct byte order in the string.
Example:
bytearray := []byte(some_string)
bytearray = append(bytearray, 0x80)
some_string = string(bytearray)
Kind of a silly work around, if anyone finds a better method, please post it.