从字节片

时间:2018-10-15 07:31:26

标签: arrays go slice bit endianness

我有以下字节切片,我需要从中提取比特并将其放置在[] 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切片来进一步访问位值。

我需要在这里使用移位运算符吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

一种方法是遍历字节,并使用第二个循环逐位移位字节值,并使用位掩码测试位。并将结果添加到输出切片中。

这是它的一个实现:

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]

答案 1 :(得分:0)

使用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相当。

相关问题