从字节片中提取位到一个 int 片中
Extract bits into a int slice from byte slice
我有以下字节片,我需要从中提取位并将它们放在 []int 中,因为我打算稍后获取各个位值。我很难弄清楚该怎么做。
下面是我的代码
data := []byte{3 255}//binary representation is for 3 and 255 is 00000011 11111111
我需要的是一小段位 --> [0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
我试过的
- 我尝试使用 BigEndian 将字节切片转换为 Uint16,然后尝试使用
strconv.FormatUint
但失败并出现错误 panic: runtime error: index out of range
- 看到许多使用
fmt.Printf
函数简单输出数字位表示的示例,但这对我没有用,因为我需要一个 int 切片来进一步访问位值。
我需要在这里使用位移运算符吗?任何帮助将不胜感激。
一种方法是遍历字节,并使用第二个循环逐位移动字节值并使用位掩码测试这些位。并将结果添加到输出切片。
这是它的一个实现:
func bits(bs []byte) []int {
r := make([]int, len(bs)*8)
for i, b := range bs {
for j := 0; j < 8; j++ {
r[i*8+j] = int(b >> uint(7-j) & 0x01)
}
}
return r
}
正在测试:
fmt.Println(bits([]byte{3, 255}))
输出(在 Go Playground 上尝试):
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1]
使用 bits
包提供了一个相当简单的解决方案。
func bitsToBits(data []byte) (st []int) {
st = make([]int, len(data)*8) // Performance x 2 as no append occurs.
for i, d := range data {
for j := 0; j < 8; j++ {
if bits.LeadingZeros8(d) == 0 {
// No leading 0 means that it is a 1
st[i*8+j] = 1
} else {
st[i*8+j] = 0
}
d = d << 1
}
}
return
}
性能相当于 similar solutions。
我有以下字节片,我需要从中提取位并将它们放在 []int 中,因为我打算稍后获取各个位值。我很难弄清楚该怎么做。
下面是我的代码
data := []byte{3 255}//binary representation is for 3 and 255 is 00000011 11111111
我需要的是一小段位 --> [0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1]
我试过的
- 我尝试使用 BigEndian 将字节切片转换为 Uint16,然后尝试使用
strconv.FormatUint
但失败并出现错误panic: runtime error: index out of range
- 看到许多使用
fmt.Printf
函数简单输出数字位表示的示例,但这对我没有用,因为我需要一个 int 切片来进一步访问位值。
我需要在这里使用位移运算符吗?任何帮助将不胜感激。
一种方法是遍历字节,并使用第二个循环逐位移动字节值并使用位掩码测试这些位。并将结果添加到输出切片。
这是它的一个实现:
func bits(bs []byte) []int {
r := make([]int, len(bs)*8)
for i, b := range bs {
for j := 0; j < 8; j++ {
r[i*8+j] = int(b >> uint(7-j) & 0x01)
}
}
return r
}
正在测试:
fmt.Println(bits([]byte{3, 255}))
输出(在 Go Playground 上尝试):
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1]
使用 bits
包提供了一个相当简单的解决方案。
func bitsToBits(data []byte) (st []int) {
st = make([]int, len(data)*8) // Performance x 2 as no append occurs.
for i, d := range data {
for j := 0; j < 8; j++ {
if bits.LeadingZeros8(d) == 0 {
// No leading 0 means that it is a 1
st[i*8+j] = 1
} else {
st[i*8+j] = 0
}
d = d << 1
}
}
return
}
性能相当于 similar solutions。