简体   繁体   English

从位集中读取位值并传输到字节数组

[英]reading bit values from bitset and transfering to byte array

I am working on building RTCM SC104 v3.1 messages for correction data of GNSS orbiters. 我正在构建RTCM SC104 v3.1消息以获取GNSS轨道器的校正数据。 A couple of fixed length messages I have had no trouble building once I figured out the data had to be sent MSB(most significant bit not byte) first. 一旦我确定必须先发送MSB(最高有效位而不是字节),就可以毫无问题地建立几个固定长度的消息。 However for some of the variable length messages it seemed simplest to build a bitset then copy to a set number of bytes. 但是,对于某些可变长度消息,似乎最简单的方法是建立一个位集,然后将其复制到一定数量的字节中。 My problem is the byte output all end up being 0x00 while I can cout to the console the bitset which appears to be building correctly. 我的问题是字节输出全部以0x00结尾,而我可以将似乎正确构建的位集提示到控制台。 Data transmitted by this standard must be conditioned first as each data type is only allowed the number of bits needed to send the maximum value allowed. 必须首先调节由该标准传输的数据,因为每种数据类型仅允许发送允许的最大值所需的位数。 For example a 64 bit float must be multiplied a power of ten to maintain the accuracy then cast to an integer of 32bit. 例如,必须将64位浮点数乘以10的幂才能保持精度,然后将其强制转换为32位整数。 From this 27 bits may be transfered to the messaged string of bits in the msb first pattern. 从这27位可以传输到msb第一模式中的消息位字符串。 However some messages add 9.25bytes per orbiters while some may add 79 bits per orbiter to the bit string. 但是,某些消息会为每个轨道器增加9.25字节,而某些消息可能会向位串中每个轨道器增加79位。 no padding zeros are allowed until the end to fill the last byte. 在填充最后一个字节的末尾之前,不允许填充零。 So I am counting the number of bits as I set them. 所以我在设置位数时在计算位数。 Then process the number of bytes in an array needed to carry all of the bits. 然后处理携带所有位所需的数组中的字节数。 I just don't seem to be getting the 1's to the bytes. 我只是似乎没有得到1的字节。 So I am filling the bitset for each channel with good data like this chunk of code: 因此,我在每个通道的位集上填充了良好的数据,例如这段代码:

    for(int varPos = 5; varPos > -1; varPos --) //start on 0, end on 5
        {
            data_1002.set(bitPos, (datastream[baseNumber].channel[n].satID & (1<<varPos))); //test bit
            bitPos++;
        }
        data_1002.set(bitPos,1);
        bitPos++;
        for(int varPos = 23; varPos > -1; varPos --) //start on 0, end on 5
        {
            data_1002.set(bitPos, (codeRange & (1<<varPos))); //test bit
            bitPos++;
        }
        for(int varPos = 19; varPos > -1; varPos --) //start on 0, end on 5
        {
            data_1002.set(bitPos, (difference & (1<<varPos))); //test bit
            bitPos++;
        }
        for(int varPos = 6; varPos > -1; varPos --) //start on 0, end on 5
        {
            data_1002.set(bitPos, (lockInd & (1<<varPos))); //test bit
            bitPos++;
        }

Then try to fill the array like this: 然后尝试像这样填充数组:

noBytes = (bitPos+7)/8; //number of data bytes to copy to array
if(noBytes <=0)
{
    noBytes = 0;
}
cout << "no bytes to build  " << noBytes << endl;

for(int w=0; w<noBytes; w++)
{
    for(int q=0; q<8; q++)
    {
        if(data_1002[bitPos+q] == true)
        {
            data = data | (1<<q);
        }
        else
        {
            data = data & (0xFF & (0<<q));
        }
    }
    bitPos = bitPos +8;
    output += data;
cout << "data byte is  ";
cout << data << endl;;
    data = 0;
}

I have also tried to test for bitset[position] == 1 and also tried '1' with no change. 我也尝试过测试bitset [position] == 1,并且也尝试了'1'而没有任何变化。 Somewhere I am messing up but I'm not sure if I'm not reading the bitset or not writing to the bytes correctly. 我在某个地方搞砸了,但是我不确定我是在读取位集还是未正确写入字节。 Please help. 请帮忙。

The error is in 错误在

data = data & (0xFF & (0<<q));

If you pay attention, 0 << q is always zero; 如果注意, 0 << q始终为零; what follows are some bitwise ANDs, so everything ends up zero. 接下来是一些按位与,因此所有结果都为零。

Try this instead: 尝试以下方法:

data = data & ~(1<<q));

Or just delete the else part, as data is zeroed every time around the outer loop anyways. 或者只是删除else部分,因为无论如何每次都在外循环周围将data清零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM