2

I run this code and get an output, but why the bytes value is E4B8AD and the int value is 20013. Why column 2 is not equal to column 5 ?

package main

import(
    "fmt"
)

func main(){

    str2 := "中文"

    fmt.Println("index int(rune) rune char bytes")
    for index, rune := range str2{
        fmt.Printf("%-2d      %d       %U '%c' %X\n", index, rune, rune, rune, []byte(string(rune)))
    }
}

the output is :

index int(rune) rune char bytes
0      20013       U+4E2D '中' E4B8AD
1      25991       U+6587 '文' E69687
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sherwinliu
  • 39
  • 2

1 Answers1

4

A Unicode code point for a character is not necessarily the same as the byte representation of that character in a given character encoding.

For the character , the code point is U+4E2D, but the byte representations in various character encodings are:

  • E4B8AD (UTF-8)
  • 4E2D (UTF-16)
  • 00004E2D (UTF-32)

There's a really good answer here that explains how to convert between code points and byte representations. There's also the excellent The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156