0

Am working on a C++ app in Windows platform. There's a unsigned char pointer that get's bytes in decimal format.

unsigned char array[160];

This will have values like this,

array[0] = 0
array[1] = 0
array[2] = 176
array[3] = 52

array[4] = 0
array[5] = 0
array[6] = 223
array[7] = 78

array[8] = 0
array[9] = 0
array[10] = 123
array[11] = 39

array[12] = 0
array[13] = 0
array[14] = 172
array[15] = 51

.......
........
.........
and so forth...

I need to take each block of 4 bytes and then calculate its decimal value.

So for eg., for the 1st 4 bytes the combined hex value is B034. Now i need to convert this to decimal and divide by 1000.

As you see, for each 4 byte block the 1st 2 bytes are always 0. So i can ignore those and then take the last 2 bytes of that block. So from above example, it's 176 & 52.

There're many ways of doing this, but i want to do it via using bit wise operators.

Below is what i tried, but it's not working. Basically am ignoring the 1st 2 bytes of every 4 byte block.

int index = 0

for (int i = 0 ; i <= 160; i++) {
        index++;
        index++;
        float Val =  ((Array[index]<<8)+Array[index+1])/1000.0f;

        index++;
    }
7
  • you cant shift the value more than its bitsize Commented Dec 21, 2018 at 14:13
  • for (i <= 160) is too big. You have only 40 of them. Commented Dec 21, 2018 at 14:17
  • 1
    So you know that you have 4 values, but you only increment 3 times? why not for(int index = 0; index < 160; index +=4)? Commented Dec 21, 2018 at 14:17
  • 1
    Please be more specific than "it's not working". Commented Dec 21, 2018 at 14:17
  • 1
    There are no "hex values" or "decimal values". (0xb0 << 8) | 0x34 is exactly the same as (176 << 8) | 52. Commented Dec 21, 2018 at 14:21

2 Answers 2

1

Since you're processing the array four-by-four, I recommend that you increment i by 4 in the for loop. You can also avoid confusion after dropping the unnecessary index variable - you have i in the loop and can use it directly, no?

Another thing: Prefer bitwise OR over arithmetic addition when you're trying to "concatenate" numbers, although their outcome is identical.

for (int i = 0 ; i <= 160; i += 4) {
    float val = ((array[i + 2] << 8) | array[i + 3]) / 1000.0f;
}
Sign up to request clarification or add additional context in comments.

1 Comment

If using bitwise operators, better to use an unsigned type than long.
1

First of all, i <= 160 is one iteration too many.

Second, your incrementation is wrong; for index, you have

Iteration 1:
    1, 2, 3
    And you're combining 2 and 3 - this is correct.
Iteration 2:
    4, 5, 6
    And you're combining 5 and 6 - should be 6 and 7.
Iteration 3:
    7, 8, 9
    And you're combining 8 and 9 - should be 10 and 11.

You need to increment four times per iteration, not three.

But I think it's simpler to start looping at the first index you're interested in - 2 - and increment by 4 (the "stride") directly:

for (int i = 2; i < 160; i += 4) {
    float Val = ((Array[i]<<8)+Array[i+1])/1000.0f;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.