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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?