Golang convert integer to unicode character

前端 未结 2 1796
难免孤独
难免孤独 2021-01-28 21:29

Given the following input:

intVal := 2612
strVal := \"2612\"

What is a mechanism for mapping to the associated unicode value as a string.

2条回答
  •  臣服心动
    2021-01-28 22:14

    2612 is not the integer value of the unicode rune, the integer value of \u2612 is 9746. The string "2612" is the hex value of the rune, so parse it as a hex number and convert it to a rune.

    i, err := strconv.ParseInt(strVal, 16, 32)
    if err != nil {
        log.Fatal(err)
    }
    r := rune(i)
    fmt.Println(string(r))
    

    https://play.golang.org/p/t_e6AfbKQq

提交回复
热议问题