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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助