2

I am reading data back from an imaging camera system, this camera detects age, gender etc, one of the values that comes back is the confidence value, this is 2 bytes, and is shown as the LSB and MSB, I have just tried converting these to integers and adding them together, but I don't get the value expected.

is this the correct way to get a value using the LSB and MSB, I have not used this before.

Thanks

Racing57
  • 81
  • 1
  • 3
  • 15
  • just as an example, I have a value of 242 if I convert the LSB to an int, and 2 for the MSB, I am sure it is a simple equation, once its pointed out. – Racing57 Mar 03 '15 at 11:53
  • You can use bitwise operators: `(myByte & 1) + ((myByte & 128) >> 7)`, that's what they're for after all. You can also perform the whole calculation without addition if you'd like. – Benjamin Gruenbaum Mar 03 '15 at 11:54

2 Answers2

7

Your value is going to be:

Value = LSB + (MSB << 8);

Explanation:

A byte can only store 0 - 255 different values, whereas an int (for this example) is 16 bits.

The MSB is the left hand^ side of the 16 bits, and as such needs to be shifted to the left side to change the bits used. You can then add the two values.

I would suggest looking up the shifting operators.

^ based on endienness (Intel/Motorola)

Sarima
  • 749
  • 7
  • 21
  • Why is an int 16 bits in this example? The inferred type of MSB << 8 is Int32. – Ceisc Mar 03 '15 at 12:15
  • this seems to work a treat, the maximum confidence reported back I 1000, and I am getting up to that value.. I do need to read up a little on this I think, as I need to understand exactly what it is doing. but thanks !!! – Racing57 Mar 03 '15 at 12:28
1

Assuming that MSB and LSB are most/least significant byte (rather than bit or any other expansion of that acronym), the value can be obtained by MSB * 256 + LSB.

Ceisc
  • 1,278
  • 12
  • 18