您的位置 首页 golang

「golang」 string和「」byte 相互转换

golang 中的string 和 []byte 相互转换

string to [] byte

 var strhello string = "hello world"
var bytehello []byte = []byte(strhello)
fmt. Printf ("%s [%x]\n", bytehello, bytehello)  

输出:

hello world [68656c6c6f20776f726c64]

[]byte to string

 var bytehello []byte = []byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
var stringhello string = string(bytehello[:])
fmt.Printf("%s [%x]\n", stringhello, stringhello)  

输出:

hello world [68656c6c6f20776f726c64]

文章来源:智云一二三科技

文章标题:「golang」 string和「」byte 相互转换

文章地址:https://www.zhihuclub.com/88661.shtml

关于作者: 智云科技

热门文章

网站地图