如何将十六进制字解析为填充的位数组

时间:2016-02-25 16:15:11

标签: c++ arrays qt

我有一个Hex字节,我需要转换为8位二进制。这是我目前的代码。

$(document).ready(function(){
   $(".navbar_item_actions").click(function(){
        var display = $(".actions-dropdown").css('display');
        if(display == 'none'){
            $(".actions-dropdown").show(400);
        } else {
            $(".actions-dropdown").hide(400);
        }
   });
});

我的问题是当我构建数组时,它将索引0填充为最重要的位。这个问题是每个十六进制数,以1读取开始作为索引0 True。我需要一个填充数组,该数组随Data_0十六进制输入而变化,我可以按位查看这些位。例如:

[0 | 0 | 0 | 0 | 0 | 0 | 0 | 1]索引0等于1 [1 | 1 | 1 | 0 | 0 | 0 | 0 | 0]索引0等于0 这些代表键盘上的灯。目前1,2,4,8,16都将指数0显示为1.

感谢。

1 个答案:

答案 0 :(得分:2)

您不需要使用字符串。 (请注意,“十六进制”不是一个值。它只是一种表示法的整数值)。
你的例子:

d2

说明:

quint8 a = /* some val */; // your 8-bit value
QBitArray b(8, 0); // bit array
for(int i=0; i<8; i++) // loop by each of 8 bits of your 8-bit value
{
    /* There I create the bitwise mask for
    each bit of your 8-bit value. 
    After that I apply it for the value */

    quint8 theMaskForEachBit = 1 << i; // movement by the order
    bool bit = a & theMaskForEachBit; // appyling the mask 
    b[i] = bit;
}

用于理解C ++中按位运算符的好文章:http://www.cprogramming.com/tutorial/bitwise_operators.html
另见:https://en.wikipedia.org/wiki/Two%27s_complement