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:
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:
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:
[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:
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:
[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:
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:
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:
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:
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!