Home >Backend Development >Golang >How to Convert a Byte Slice to an Int Slice in Go?
Converting a byte slice to an integer slice can be easily accomplished by iterating over each byte value and converting it to an integer.
byteSlice := []byte{1, 2, 3, 4} intSlice := make([]int, len(byteSlice)) for i, b := range byteSlice { intSlice[i] = int(b) }
In the code above, the range loop iterates over each byte in the byteSlice. For each byte, it converts the byte to an integer using the int() function. The resulting integer value is stored in the corresponding element of the intSlice.
It's worth noting that the slice already contains a byte value of 1, not the ASCII character 1. Therefore, there is no need to convert it to a string.
For more detailed information on conversions, refer to the following resources:
The above is the detailed content of How to Convert a Byte Slice to an Int Slice in Go?. For more information, please follow other related articles on the PHP Chinese website!