美文网首页
11:golang UTF8与UTF16互转

11:golang UTF8与UTF16互转

作者: 陈玉涛 | 来源:发表于2020-01-03 09:10 被阅读0次
    package main
     
    import (
        "golang.org/x/text/encoding/unicode"
        "golang.org/x/text/transform"
        "fmt"
    )
    
    func main() {
        bs_UTF16LE, _, _ := transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewEncoder(), []byte("测试"))
        bs_UTF16BE, _, _ := transform.Bytes(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder(), []byte("测试"))
        bs_UTF8LE, _, _ := transform.Bytes(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder(), bs_UTF16LE)
        bs_UTF8BE, _, _ := transform.Bytes(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder(), bs_UTF16BE)
    
        fmt.Printf("%v\n%v\n%v\n%v\n", bs_UTF16LE, bs_UTF16BE, bs_UTF8LE, bs_UTF8BE)
    }
    

    相关文章

      网友评论

          本文标题:11:golang UTF8与UTF16互转

          本文链接:https://www.haomeiwen.com/subject/rjzaoctx.html