AGRAJA wrote:
how to convert unsigned to char?
Use the cast operator: (char)
Ref: http://www.thescripts.com/forum/thread477545.html
how do I print without the leading ffffff (yet the result should be
char*)?
Printing a pointer type requires the "%p" format specifier. On the
machines I use most often, getting lots of leading 'f's is pretty much
unavoidable when using %p with valid pointer values. However, are you
sure you want the result to be char*? In context, I'd have expected that
you wanted to print a char, not a char*.
If that's the case, then you should use either "%d" or "%u" format
specifier, depending upon whether or not char is signed (if CHAR_MIN <
0, then char is a signed type). Either way, you're absolutely guaranteed
to not get any 'f's in the output. :-)
However, I suspect that what you really want is to get no 'f's despite
using the "%x" specifier. On most implementations where 'char' is
unsigned, you're extremely unlikely to get leading f's when printing a
char value.
Therefore, I assume that you're using an implementation where 'char' is
signed. You still won't get many leading 'f's on most implementations ,
unless the value you're converting to 'char' is greater than CHAR_MAX.
Don't do that! Performing such a conversion will either generate an
implementation-defined result, or cause an implementation-defined signal
to be raised. Either way, it's generally not a good thing to do. It's
likely to result in a negative number; if you convert that number to
unsigned, so that it can be printed using the "%x" format, then the
conversion will generally result in lots of leading 'f's.
I'd recommend using 'unsigned char' rather than 'char' for such purposes.