Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sprintf an unsigned char?

This doesn't work:

unsigned char foo;
foo = 0x123;

sprintf("the unsigned value is:%c",foo);

I get this error:

cannot convert parameter 2 from 'unsigned char' to 'char'

like image 682
Christoferw Avatar asked Jan 12 '10 16:01

Christoferw


2 Answers

Before you go off looking at unsigned chars causing the problem, take a closer look at this line:

sprintf("the unsigned value is:%c",foo);

The first argument of sprintf is always the string to which the value will be printed. That line should look something like:

sprintf(str, "the unsigned value is:%c",foo);

Unless you meant printf instead of sprintf.

After fixing that, you can use %u in your format string to print out the value of an unsigned type.

like image 83
MAK Avatar answered Sep 18 '22 15:09

MAK


Use printf() formta string's %u:

printf("%u", 'c');
like image 36
Ariel Avatar answered Sep 20 '22 15:09

Ariel