Joining hex values to create a 16bit value

柔情痞子 提交于 2019-12-11 15:32:27

问题


Im receiving three uint8 values which are the Most, Middle and Least Significant Digits of a plot value:

EG: Printed in console (%c):

   1 A 4

I need to pass them into a signal view UI grapher which accepts a uint16_t. So far the way im doing it is not working correctly.

 uint16_t iChanI = (bgp->iChanIH << 8) + (bgp->iChanIM <<4 ) + bgp->iChanIL;
 uint16_t iChanQ = (bgp->iChanQH << 8) + (bgp->iChanQM <<4) + bgp->iChanQL;

[self updateSView:iChanI ichanQ:iChanQ];

Am i merging them correctly, or just adding the values?

Any help is much appreciated,

Thanks,


回答1:


You first need to convert each hex character to its equivalent 4 bit (nybble) representation, and then merge them into an int16_t, e.g.

uint8_t to_nybble(char c)
{
    return 'c' >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
}

uint16_t iChanI = (to_nybble(bgp->iChanIH) << 8) |
                  (to_nybble(bgp->iChanIM) << 4) |
                   to_nybble(bgp->iChanIL);


来源:https://stackoverflow.com/questions/17045908/joining-hex-values-to-create-a-16bit-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!