Example of converting utf8 to a rune (integer)

This Go program converts a UTF-8 string to a rune.

package main

import "fmt"
import "unicode/utf8"

func main() {
        var r rune
        kan := []byte ("缶")

        fmt.Printf ("%s\n", kan)
        r, _ = utf8.DecodeRune (kan)
        fmt.Printf ("Rune is %X\n", r)
}

(download)

The output looks like this:

缶
Rune is 7F36


Copyright © Ben Bullock 2009-2024. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer