Why does C print my hex values incorrectly?

后端 未结 3 1897
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 21:28

So I\'m a bit of a newbie to C and I am curious to figure out why I am getting this unusual behavior.

I am reading a file 16 bits at a time and just printing them ou

3条回答
  •  星月不相逢
    2020-12-17 22:22

    The placeholder %x in the format string interprets the corresponding parameter as unsigned int.

    To print the parameter as short, add a length modifier h to the placeholder:

    printf("%hx", hex);
    

    http://en.wikipedia.org/wiki/Printf_format_string#Format_placeholders

提交回复
热议问题