How to print 1 byte with printf?

后端 未结 4 636
难免孤独
难免孤独 2020-12-31 08:17

I know that when using %x with printf() we are printing 4 bytes (an int in hexadecimal) from the stack. But I would like to print only

4条回答
  •  渐次进展
    2020-12-31 08:51

    If you want to print a single byte that is present in a larger value type, you can mask and/or shift out the required value (e.g. int x = 0x12345678; x & 0x00FF0000 >> 16). Or just retrieve the required byte by casting the needed byte location using a (unsigned) char pointer and using an offset.

提交回复
热议问题