Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does printf not print out just one byte when printing hex?

Tags:

c++

c

printf

pixel_data is a vector of char.

When I do printf(" 0x%1x ", pixel_data[0] ) I'm expecting to see 0xf5.

But I get 0xfffffff5 as though I was printing out a 4 byte integer instead of 1 byte.

Why is this? I have given printf a char to print out - it's only 1 byte, so why is printf printing 4?

NB. the printf implementation is wrapped up inside a third party API but just wondering if this is a feature of standard printf?

like image 302
BeeBand Avatar asked Aug 24 '10 11:08

BeeBand


People also ask

How do you printf in hexadecimal?

To print integer number in Hexadecimal format, "%x" or "%X" is used as format specifier in printf() statement. "%x" prints the value in Hexadecimal format with alphabets in lowercase (a-f). "%X" prints the value in Hexadecimal format with alphabets in uppercase (A-F).

How do I printf a byte?

Assumption:You want to print the value of a variable of 1 byte width, i.e., char . In case you have a char variable say, char x = 0; and want to print the value, use %hhx format specifier with printf() . printf("%x", x);

What format specifier should be used to print a hex value?

Here, following format specifiers are used: %d - to print value in integer format. %o - to print value in octal format. %x - to print value in hexadecimal format (letters will print in lowercase)

What should be included in printf?

To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement. The return 0; statement inside the main() function is the "Exit status" of the program. It's optional.


2 Answers

You're probably getting a benign form of undefined behaviour because the %x modifier expects an unsigned int parameter and a char will usually be promoted to an int when passed to a varargs function.

You should explicitly cast the char to an unsigned int to get predictable results:

printf(" 0x%1x ", (unsigned)pixel_data[0] ); 

Note that a field width of one is not very useful. It merely specifies the minimum number of digits to display and at least one digit will be needed in any case.

If char on your platform is signed then this conversion will convert negative char values to large unsigned int values (e.g. fffffff5). If you want to treat byte values as unsigned values and just zero extend when converting to unsigned int you should use unsigned char for pixel_data, or cast via unsigned char or use a masking operation after promotion.

e.g.

printf(" 0x%x ", (unsigned)(unsigned char)pixel_data[0] ); 

or

printf(" 0x%x ", (unsigned)pixel_data[0] & 0xffU ); 
like image 51
CB Bailey Avatar answered Sep 20 '22 01:09

CB Bailey


Better use the standard-format-flags

printf(" %#1x ", pixel_data[0] ); 

then your compiler puts the hex-prefix for you.

like image 44
user411313 Avatar answered Sep 23 '22 01:09

user411313