Home  >  Article  >  Backend Development  >  byte to string golang

byte to string golang

WBOY
WBOYOriginal
2023-05-10 13:24:391386browse

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.

  1. Use the strconv package

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.

  1. Use the string() method

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.

  1. Convert byte array to string

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
}
  1. string to byte array

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:

  1. Chinese characters

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.

  1. The length of the original string and the new string

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:golang flow chartNext article:golang flow chart