drlhsfqoa350437979 2016-01-18 18:20
浏览 53
已采纳

如何在Go中检测何时无法将字节转换为字符串?

There are invalid byte sequences that can't be converted to Unicode strings. How do I detect that when converting []byte to string in Go?

  • 写回答

1条回答 默认 最新

  • dpbf62565 2016-01-18 20:08
    关注

    You can, as Tim Cooper noted, test UTF-8 validity with utf8.Valid.

    But! You might be thinking that converting non-UTF-8 bytes to a Go string is impossible. In fact, "In Go, a string is in effect a read-only slice of bytes"; it can contain bytes that aren't valid UTF-8 which you can print, access via indexing, or even round-trip back to a []byte (to Write, say).

    There are two places in the language that Go does do UTF-8 decoding of strings for you.

    • when you do for i, r := range s the r is a Unicode code point as a value of type rune
    • when you do the conversion []rune(s), Go decodes the whole string to runes

    In both these instances invalid UTF-8 is replaced with U+FFFD, the replacement character reserved for uses like this. More is in the spec sections on for statements and conversions between strings and other types. These conversions never crash, so you only need to actively check for UTF-8 validity if it's relevant to your application, like if you want to throw an error on mis-encoded input.

    Since that behavior's baked into the language, you can expect it from libraries, too. U+FFFD is utf8.RuneError and returned by functions in utf8.

    Here's a sample program showing what Go does with a []byte holding invalid UTF-8:

    package main
    
    import "fmt"
    
    func main() {
        a := []byte{0xff}
        s := string(a)
        fmt.Println(s)
        for _, r := range s {
            fmt.Println(r)
        }
        rs := []rune(s)
        fmt.Println(rs)
    }
    

    Output will look different in different environments, but in the Playground it looks like

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!