How to print unsigned char as a two-digit hex value in C?

I am trying to print an unsigned char value as a six-digit value in 2 digits, but always get the result as 4-digit hexadecimal values, not sure what is wrong with my code.

// unsigned char declaration unsigned char status = 0x00; // printing out the value printf("status = (0x%02X)\n\r", (status |= 0xC0)); 

I expect a two-digit hexadecimal result as 0xC0 , but always get 0xC0FF .

Also, when I tried to print the same variable (status) as unsigned char with the format identifier %bu , I got the result as 255 .

How do you get only two hexadecimal characters as output?

+5
source share
4 answers

As far as I know, the Keil C compiler does not fully comply with the C standard. If so, it is likely that it does not quite comply with the standard promotion rules for such things as passing char values ​​to variable functions; on an 8-bit processor there are performance advantages when non-propagating 8-bit values ​​up to 16 bits or more.

As a workaround, you can explicitly truncate the high-order bits before passing the argument to printf . Try the following:

 #include <stdio.h> int main(void) { unsigned char status = 0x00; status |= 0xC0; printf("status = 0x%02X\n", (unsigned int)(status & 0xFF)); return 0; } 

Performing bitwise "and" with 0xFF clears everything except the lower 8 bits; casting to unsigned int does not have to be necessary, but it ensures that the argument is actually of the type expected by printf with the format "%02X" .

You should also refer to your implementation documentation regarding non-standard behavior for printf type advertising campaigns.

+13
source

you send a char to a format string that expects an int. The printf function grabs another bit from the stack to fill it. Try

  printf("%02X",(int)(status|0xC0)); 
+3
source

Pass it to an unsigned char:

 printf("status = (0x%02X)\n\r", (unsigned char)(status |= 0xC0)); 
+1
source

Looking at all the answers, I think that maybe we are missing another way to do this.

 const unsigned char chararr[]="abceXYZ"; for (int i=0; i< 7; ++i) { printf("%#04X %d %c\n", chararr[i], chararr[i], chararr[i]); } 0X61 97 a 0X62 98 b 0X63 99 c 0X65 101 e 0X58 88 X 0X59 89 Y 0X5A 90 Z 

If you use% # 04x small x, then the output will be b 0x small x prefix. The # pound sign tells the function to print 0x. 04 to indicate how many digits to print, if the input is set to "0x0a", it will print it, and without 04 - "0xa".

On my computer, a Dell workstation, the result is as expected. If not

 unsigned char status = 0x00; printf("status = (0x%02X)\n\r", (status |= 0xC0)); // output //status = (0xC0) // is exactly expected by the original question. 

It is better to illustrate with examples:

  37 printf("status = (%#02x)\n", (status |= 0xC0)); 38 printf("status = (%#04x)\n", (status |= 0xC0)); 39 printf("status = (%#04x)\n", 0x0f); 40 printf("status = (%#02x)\n", 0x0f); status = (0xc0) status = (0xc0) status = (0x0f) status = (0xf) 
0
source

Source: https://habr.com/ru/post/1433280/


All Articles