golang

Golang String to Byte and Vice Versa

We define a string in go as a read-only slice of bytes. A string can hold Unicode Text in UTF-8 encoding or other pre-defined format. But since a string is basically a slice of bytes, it does not necessarily have to hold such types.

In this article, you will learn how to convert a string to a byte array and a byte array to a string. Keep in mind that this article does not serve as an introduction to strings or bytes in Go.

Convert String to Byte

Before we learn how to convert a string to a byte, let us define what a byte is. A byte refers to an 8-bit unsigned integer. Bytes are very common when working with slices.

In go, we can convert a string to a byte using the byte() function.

The function syntax is as shown:

[]byte(string)

The function takes the string as the argument. The function returns a slice with all the bytes of the characters in the specified string.

Let us look at an example:

package main
import "fmt"
func main() {
    str := "Linuxhint"
    byte_arr := []byte(str)
    fmt.Println(byte_arr)
}

In the example above, we define a variable called “str” with the value “Linuxhint”.

We then use the byte() method to convert the string to a byte array.

The code above returns an output as:

$ go run string_to_byte.go
[76 105 110 117 120 104 105 110 116]

Copy String to Byte Slice

We can also convert a string to a byte by copying a string to a byter slice. We accomplish this using the copy() method.

The code snippet below shows how to achieve this:

package main
import "fmt"
func main() {
    copy_string()
}
func copy_string() {
    // empty slice
    byte_slice := make([]byte, 10)

    str := "Linuxhint"
    str_copy := copy(byte_slice, str)
    fmt.Println(str_copy)
    fmt.Println(byte_slice)
}

In the example above, we create an empty slice using the make function.

We then use the copy() method to copy the string to the byte slice. We can see the number of bytes copied to the slice using the fmt.Println(str_copy).

To view the full string in bytes, use the fmt.Println(byte_slice):

An example output is as shown:

9 // copied bytes
[76 105 110 117 120 104 105 110 116 0] // byte slice

Convert Byte to String

The first method we can use to convert a byte array to a string is the NewBuffer() method. This creates a new buffer, and then we can use the String() method as shown in the example below:

package main

import (
    "bytes"
    "fmt"
)
func main() {
    arr := []byte{'L', 'i', 'n', 'u', 'x'}
    str := bytes.NewBuffer(arr).String()
    fmt.Println(str)
}

We start by creating a byte array. We then use the NewBuffer() method to create a new buffer and then use the String() method to get the string output.

The resulting function is as:

$ go run byte_to_string.go
Linux

Convert Byte to String with Slicing

Another technique we can use to convert a byte array to string is slicing. An example illustrates how to use this method:

func slicing() {
    arr := []byte{'L', 'i', 'n', 'u', 'x'}
    str := string(arr[:])
    fmt.Println(str)
}

The above code should take the byte array and convert it to a string.

Convert Byte to String using the Sprintf() method

The Sprintf() function allows you to convert a byte array to a string. Consider the example shown below:

func sprintf_method() {
    arr := []byte{'L', 'i', 'n', 'u', 'x'}
    str := fmt.Sprintf("%s", arr)
    fmt.Println(str)
}

The above method should convert the byte array to a string. Keep in mind that this method is regarded slower compared to other options.

Conclusion

In this guide, we explored the world of the Go programming language and how to convert a byte to a string and vice versa.

Happy Coding!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list