Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining 17bit data into byte array

I'm having a bit of an issue with trying to move groups of 17bit data in to a byte array. I don't want to have to go through step-by-step, but I can't figure out a logical loop.
I need it this way because I'm meant to calculate a checksum by adding up the all the byte values after combining them like this.
So here is what I am struggling with.

I have 16 byte arrays. The first 3 bytes of the array contain the 17 bits I'm after. (8 bits from [0], 8 bits from [1], and the MSB from [2].)

I need to move these 16 17bit values to one separate byte array.

The first one is easy:

int index = 0;
myArray[index++] = driverData[driver][0];            //First byte
myArray[index++] = driverData[driver][1];            //Second byte
myArray[index] = (driverData[driver][2] & 0x80) << 7;  //First bit of the third byte.

From here though it gets harder to attempt any kind of loop to move these over.

driver++;<br>
//Take the 7 MSBs from the data array.
myArray[index++] |= (byte)(driverData[driver][0] & 0x7e >> 1);
//This leaves a single bit left over on driverData[driver][0].
myArray[index] = (byte)(driverData[driver][1] & 0x1 << 7);

I think you get the picture. Am I doing this all wrong? Can anyone point me in the right direction?

like image 247
Spenduku Avatar asked Jul 22 '26 15:07

Spenduku


2 Answers

Sounds like you have a prime number loop large enough to make coding the individual cases a bad idea. This is a classic packing problem. You need a loop that iterates through your destination, and some inner code that gets more bits to pack. Your packing code should know how many bits are available to it from the last iteration, how many it needs, and should be able to increment the source pointer if it doesn't have enough.

like image 122
Variable Length Coder Avatar answered Jul 24 '26 05:07

Variable Length Coder


OK, so this looks to be working. I probably need to test it more, but this seems to be giving me the result I expect so far. I'm sure I could do this better somehow.

// ... //
void foo()
{
    //Lets start by getting all the 17bit values from each driver for the board.
    int bitIndex = 7;
    int byteIndex = 0;
    int stopIndex = chipIndex + GetChipCount();
    //Now we start the shiftyness.
    for (int driver = chipIndex; driver < stopIndex; driver++) {
        int userBits =
            (driverData[driver][0] & 0xff) << 9 | (driverData[driver][1]
                               & 0xff)
            << 1 | (driverData[driver][2] & 0x80) >> 7;
        AddBitsToArray(userBits, ref bitIndex, ref byteIndex);

    }
}

/// <summary>
/// Takes the 17 bits, and adds them to the byte array.
/// </summary>
private void AddBitsToArray(int userBits, ref int bitIndex, ref int byteIndex)
{
    int bitCount = 17;
    while (bitCount > 0) {
        //First 8 bytes.
        checksumBytes[byteIndex] |=
            (byte) (((userBits & bitValue(bitCount - 1)) >>
                 (bitCount - 1)) << bitIndex);
        //Move up the bit index to be written to.
        bitIndex--;
        //Decrement the number of bits left to shift.
        bitCount--;
        //If we have gone past the 8th bit, reset the bitIndex and increment the byteIndex.
        if (bitIndex >= 0)
            continue;
        bitIndex = 7;
        byteIndex++;
    }
}

/// <summary>
/// Returns the value of a single bit at the given index.
/// </summary>
private int bitValue(int bitIndex)
{
    return (int)(Math.Pow(2, bitIndex));
}
like image 28
Spenduku Avatar answered Jul 24 '26 04:07

Spenduku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!