I'm working with binary input in Go and I need to convert a byte into a string. This works most of the time except when when I run into a byte with a 1 as the MSB. When this happens it turns the byte into a 2 length string.
For example:
Input byte 0b11111111 would turn into a two length string of 11000011 10111111
or 0b10000000 would turn into 11000010 10000000
but 0b01000000 turns into a single character string 01000000 as wanted.
Is there anyway to get it to convert to just a single character string even with a 1 in the MSB?
CodePudding user response:
Use the following to convert a single byte b to a string containing the single byte b:
str := string([]byte{b})
The conversion string(b) yields a string containing the UTF-8 representation of b. The UTF-8 representation of a byte with the MSB set occupies two bytes.
