Convert a byte to a string in Go
Similar to inf's suggestion but allowing for commas:
fmt.Sprintf("%d,%d,%d,%d", bytes[0], bytes[1], bytes[2], bytes[3])
If you are not bound to the exact representation then you can use fmt.Sprint
:
fmt.Sprint(bytes) // [1 2 3 4]
On the other side if you want your exact comma style then you have to build it yourself using a loop together with strconv.Itoa
.
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,",")
}
to be called by:
bytes := [4]byte{1,2,3,4}
str := convert(bytes[:])