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)


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) or use the discussion group at Google Groups. / Privacy / Disclaimer