在 Go 中将字节转换为字符串 [英] Convert a byte to a string in Go
本文介绍了在 Go 中将字节转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做这样的事情:
I am trying to do something like this:
bytes := [4]byte{1,2,3,4}
str := convert(bytes)
//str == "1,2,3,4"
我搜索了很多,但真的不知道该怎么做.
I searched a lot and really have no idea how to do this.
我知道这行不通:
str = string(bytes[:])
推荐答案
不是最有效的实现方式,但你可以简单地写:
Not the most efficient way to implement it, but you can simply write:
func convert( b []byte ) string {
s := make([]string,len(b))
for i := range b {
s[i] = strconv.Itoa(int(b[i]))
}
return strings.Join(s,",")
}
被调用:
bytes := [4]byte{1,2,3,4}
str := convert(bytes[:])
这篇关于在 Go 中将字节转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文