How to convert an array of byte to UTF8?
I have a file which is latin
with the content: "ø"
<code>f, _ := os.Open(file)
b, _ := io.ReadAll(f)
for _, r := range b {
fmt.Printf("char: %s %d %xn", string(r), r, r)
//buf := make([]byte, utf8.UTFMax)
//n := utf8.EncodeRune(buf, r)
//fmt.Printf("%xn", buf[:n])
}
</code>
<code>f, _ := os.Open(file)
b, _ := io.ReadAll(f)
for _, r := range b {
fmt.Printf("char: %s %d %xn", string(r), r, r)
//buf := make([]byte, utf8.UTFMax)
//n := utf8.EncodeRune(buf, r)
//fmt.Printf("%xn", buf[:n])
}
</code>
f, _ := os.Open(file)
b, _ := io.ReadAll(f)
for _, r := range b {
fmt.Printf("char: %s %d %xn", string(r), r, r)
//buf := make([]byte, utf8.UTFMax)
//n := utf8.EncodeRune(buf, r)
//fmt.Printf("%xn", buf[:n])
}
output
<code>char: " 34 22
char: ø 248 f8
char: " 34 22
</code>
<code>char: " 34 22
char: ø 248 f8
char: " 34 22
</code>
char: " 34 22
char: ø 248 f8
char: " 34 22
If I uncomment the three lines this error is returned
<code>cannot use r (variable of type byte) as rune value in argument to utf8.EncodeRune
</code>
<code>cannot use r (variable of type byte) as rune value in argument to utf8.EncodeRune
</code>
cannot use r (variable of type byte) as rune value in argument to utf8.EncodeRune
0