Home > Article > Backend Development > How to Print Unsigned Char as Hex in C Using Ostream?
Printing Unsigned Char as Hex in C Using Ostream
To print unsigned 8-bit variables (unsigned char or uint8_t) in hexadecimal using ostream in C , consider the following solutions:
Using Casting with Hex Manipulator
You can use a cast to convert the unsigned char to an int before using the hex manipulator:
Using Custom Manipulator
MartinStettner provided an elegant custom manipulator called hexchar that simplifies hex printing:
This will produce the desired output:
Using Macro (Less Preferred)
As an alternative, you can define a macro to automate the process, though this is less idiomatic in C :
The above is the detailed content of How to Print Unsigned Char as Hex in C Using Ostream?. For more information, please follow other related articles on the PHP Chinese website!