Home > Article > Backend Development > byte to string golang
In Golang, the byte type is a primitive data type, usually used to represent single byte data, such as byte streams, files, etc. The string type is a composite type used to represent strings. When we need to convert byte type data to string type, we can use the conversion function in the Golang standard library.
1. Two ways to convert byte to string
In Golang, there are two ways to convert a byte array to string type, namely using the strconv package and using the string() method.
The strconv package is one of the standard libraries for type conversion in Golang. The string() function can convert a byte array into a string type. . The usage is as follows:
package main import ( "fmt" "strconv" ) func main() { bytes := []byte{97, 98, 99, 100, 101} // a b c d e str := string(bytes) fmt.Println("str: ", str) // 输出:str: abcde str2 := strconv.Itoa(12345) fmt.Println("str2: ", str2) // 输出:str2: 12345 }
In the above code, we define an array of byte type, convert it into a string type variable str, and output its value. In addition, we also used the Itoa() function from the strconv package to convert the integer type to the string type and output its value.
In Golang, the string type itself is actually the packaging type of the byte array, so the string() method can be used to convert the byte array into String. The usage is as follows:
package main import ( "fmt" ) func main() { bytes := []byte{97, 98, 99, 100, 101} // a b c d e str := string(bytes) fmt.Println("str: ", str) // 输出:str: abcde }
In the above code, we also define an array of byte type, convert it into a string type variable str, and output its value. The string() method is used here for conversion.
2. Conversion between byte arrays and strings
In addition to converting individual byte data into strings, we can also convert arrays containing multiple byte data into strings. Or convert string to byte array.
To convert byte array to string, you can use the two methods introduced above. The code implementation is as follows:
package main import ( "fmt" ) func main() { bytes := []byte{97, 98, 99, 100, 101} // a b c d e str := string(bytes) fmt.Println("str: ", str) // 输出:str: abcde }
To convert a string into a byte array, you need to use the []byte() function in the Golang standard library. The usage is as follows:
package main import ( "fmt" ) func main() { str := "hello world" bytes := []byte(str) fmt.Println("bytes: ", bytes) // 输出:bytes: [104 101 108 108 111 32 119 111 114 108 100] }
In the above code, we first define a string variable, convert it into a byte array, and output its value.
3. Precautions
In the process of using byte to string, you need to pay attention to the following issues:
Since Chinese characters may occupy multiple bytes, when converting a byte array to a string, you need to determine the encoding format of the byte array. For example, under UTF-8 encoding, Chinese characters usually occupy 3 bytes. Therefore, you need to pay attention to the encoding format and character length during the conversion process.
When using the string() method for conversion, you need to pay attention to the length of the original string and the new string. If the original string contains The NULL character ('
The above is the detailed content of byte to string golang. For more information, please follow other related articles on the PHP Chinese website!