Search code examples
mergehexstm32

Merge of 8-bits data into 16-bits giving wrong result for a specific number all the time


I'm getting some 8 bit data from a sensor and trying to merge it into 16 bits. But my code on stm32, doesn't work on 0x63 and 0x91 values. All the other values working except these. Result is 0x6291. What is causing this? enter image description here

I tried to merge 0x63 and 0x91 and expected 0x6391 value but I get 0x6291.


Solution

  • before i answer your question i highly recommend you to post a piece of code so we can run it and correct it for you, just to make things easy. now to answer ur question:

    1. it's not happing only in those 2 values, you can check 0x82 0xf5 just the value before 0x63, they changed
    2. u are using "+" to merge 2 hex values, u need to use add bitwise "|" and not "+", keep in mind, add is not merge
    3. try to use mask after using shift, like: "(HexValue<<8)&0xff00"