[SOLVED] Convert a byte to a string in 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.

I know this will not work:

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,",")

to be called by:

bytes := [4]byte{1,2,3,4}
str := convert(bytes[:])

Answered By – Didier Spezia

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *