Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does Go interpret byte as Unicode code point when appending to string?

I need to create a string consisting of a single byte corresponding to an integer of at most 255. It is acceptable that the string is not valid Unicode.

Code:

import (
    "fmt"
    "strings"
)

func main() {
    n := 255

    s := ""
    s += string(byte(n))
    fmt.Printf("Method 1: %x\n", s)

    sb := strings.Builder{}
    sb.WriteByte(byte(n))
    fmt.Printf("Method 2: %x\n", sb.String())
}

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Method 1: c3bf
Method 2: ff

In the first method, Go appends ÿ to the string because 255 is the code point for this Unicode character.

In the second method, Go appends the 0xff (255) byte to the string. This is the desired result, but is there a simpler way to accomplish this without needing to import the strings package? Why does Go interpret the byte as a Unicode code point in the first method?

>Solution :

In the first method, you are converting a byte to a string, which interprets that byte as a unicode code point first.

https://go.dev/ref/spec#Conversions

You can always convert a string to bytes, add a byte, and convert it back to a string:

str := string(append([]byte(sourceStr), byteValue))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading