dongshan4878 2018-02-10 17:13
浏览 401
已采纳

从符文/ int8数组转换为字符串,反之亦然

I'm porting a library from Java to Go. This library passes all parameters and returns as strings, and I must maintain this way due subsequent steps. I noticed when I cast a rune/int8 array to string and I convert back to rune/int8 array I get different values. I believe that is caused by Unicode characters. Is there a way to get the same values?

package main

import "fmt"

func main() {  

    runes := make([]rune,3)
    runes[0] = 97
    runes[1] = -22
    runes[2] = 99

    s := string(runes)

    fmt.Println(runes)  
    for _,r := range(s) {
        fmt.Println(r)
    }
}

Output:

[97 -22 99]
97
65533
99
  • 写回答

1条回答 默认 最新

  • douxuan0698 2018-02-10 21:07
    关注

    The Go Programming Language Specification

    Conversions

    Conversions to and from a string type

    Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

    Converting a slice of runes to a string type yields a string that is the concatenation of the individual rune values converted to strings.


    Type byte in Go is an alias for type uint8.

    Type rune, a Unicode code point (24-bit unsigned integer), is an alias for int32.

    Go encodes Unicode code points (runes) as UTF-8 encoded strings.

    For your example,

    package main
    
    import (
        "fmt"
        "unicode"
    )
    
    func main() {
    
        // Unicode code points are 24-bit unsigned integers
        runes := make([]rune, 3)
        runes[0] = 97
        runes[1] = -22 // invalid Unicode code point
        runes[2] = 99
        fmt.Println(runes)
    
        // Encode Unicode code points as UTF-8
        // Invalid code points converted to Unicode replacement character (U+FFFD)
        s := string(runes)
        fmt.Println(s)
    
        // Decode UTF-8 as Unicode code points
        for _, r := range s {
            fmt.Println(r, string(r), r == unicode.ReplacementChar)
        }
    }
    

    Playground: https://play.golang.org/p/AZUBd2iZp1F

    Output:

    [97 -22 99]
    a�c
    97 a false
    65533 � true
    99 c false
    

    References:

    The Go Programming Language Specification

    The Go Blog: Strings, bytes, runes and characters in Go

    The Unicode Consortium

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置