英文:
How to convert a slice of a byte array to a uint32 value
问题
我无法将字节数组的切片转换为uint值。我知道将切片传递给函数是一种更符合惯用方式的方法,但我遇到了以下问题:
- 仅打印切片中的单个元素(它打印的是[102]而不是102),基本上将其作为字节数组而不是字节打印。
- 将字节数组的切片(1个元素)转换为uint32(binary.BigEndian.Uint32似乎适用于整个数组,但不适用于数组中的单个元素)。
- 将字节数组的切片(1个元素)转换为字节。
- 将字节数组的切片(1个元素)强制转换为uint32变量。
我在下面的注释中包含了错误信息。编译器给出了错误的原因,但是如何将这些切片转换为变量呢?
以下是代码:
package main
import (
"encoding/binary"
"fmt"
)
const FrameBitsIV = 0x10
func main() {
// str := "ab£"
data := []byte{102, 97, 108, 99, 111, 110}
uint_value := []uint8{1, 2, 3, 4, 5}
x := data[0:]
fmt.Println(x)
pass_slice(data[0:], uint_value[0:])
}
func pass_slice(buf []byte, uint_value []uint8) {
x := buf[0:1]
fmt.Println(x) // 打印 [102]
y := binary.BigEndian.Uint32(x)
fmt.Println(y) // 打印错误
var z byte = buf[0:1] // 无法将buf[0:1](类型为[]byte的值)用作变量声明中的字节值
u := uint32(buf[0:]) // 无法将buf[0:](类型为[]byte的值)转换为uint32
}
非常感谢。
英文:
I am unable to convert a slice of a byte array to a uint value. I have seen that passing in slices to functions is a more idiomatic way to go but I have the following problems:
- Print only a single element from the slice (it prints [102] instead of 102) basically printing it as a byte array instead of a byte
- Convert a slice of the byte array (1 element) to a uint32 (binary.BigEndian.Uint32 seems to work for the whole array but not just a single element in the array)
- Convert a slice of the byte array (1 element) to a byte
- Cast a slice of the byte array (1 element) to a uint32 variable
I have included the errors as comments below. The compiler gives the error as to why, but how do I cast/convert from these slices to a variable?
Below is the code:
package main
import (
"encoding/binary"
"fmt"
)
const FrameBitsIV = 0x10
func main() {
// str := "ab£"
data := []byte{102, 97, 108, 99, 111, 110}
uint_value := []uint8{1, 2, 3, 4, 5}
x := data[0:]
fmt.Println(x)
pass_slice(data[0:], uint_value[0:])
}
func pass_slice(buf []byte, uint_value []uint8) {
x := buf[0:1]
fmt.Println(x) //prints [102]
y := binary.BigEndian.Uint32(x)
fmt.Println(y) //prints error
var z byte = buf[0:1] //cannot use buf[0:1] (value of type []byte) as byte value in variable declaration
u := uint32(buf[0:]) //cannot convert buf[0:] (value of type []byte) to uint32
}
Thank you so much.
答案1
得分: 1
- 从切片中只打印一个元素:
- 将字节数组的切片(1个元素)转换为字节
使用索引表达式(index expression)将字节切片的元素作为字节获取:
x := buf[0] // x 的类型是 byte
fmt.Println(x) // 打印 102
- 将字节数组的切片(1个元素)转换为 uint32。
- 将字节数组的切片(1个元素)强制转换为 uint32 变量
使用索引表达式(index expression)获取字节,并进行类型转换(convert)为 uint32:
x := uint32(buf[0]) // x 的类型是 uint32
fmt.Println(x) // 打印 102
请注意,本答案中使用的索引表达式(index expressions)与问题中使用的切片表达式(slice expressions)之间的区别。
英文:
> - Print only a single element from the slice:
> - Convert a slice of the byte array (1 element) to a byte
Use an index expression to get an element of the byte slice as a byte:
x := buf[0] // x is type byte
fmt.Println(x) //prints 102
> - Convert a slice of the byte array (1 element) to a uint32.
> - Cast a slice of the byte array (1 element) to a uint32 variable
Use an index expression to get the byte and convert to uint32:
x := uint32(buf[0]) // x is type uint32
fmt.Println(x) //prints 102
Note the difference between the index expressions used in this answer and the slice expressions used in the question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论