简体   繁体   中英

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. 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. 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. 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. From this 27 bits may be transfered to the messaged string of bits in the msb first pattern. However some messages add 9.25bytes per orbiters while some may add 79 bits per orbiter to the bit string. 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. 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. 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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