The easiest way is to use the “%X”
specifier in the
printf()
family of functions. In C++, you can set
the ios::hex
flag on a stream object before outputting
a value, then clear the flag afterward.
Here is a function called
spc_print_hex()
that prints arbitrary data of a specified length in formatted
hexadecimal:
#include <stdio.h> #include <string.h> #define BYTES_PER_GROUP 4 #define GROUPS_PER_LINE 4 /* Don't change these */ #define BYTES_PER_LINE (BYTES_PER_GROUP * GROUPS_PER_LINE) void spc_print_hex(char *prefix, unsigned char *str, int len) { unsigned long i, j, preflen = 0; if (prefix) { printf("%s", prefix); preflen = strlen(prefix); } for (i = 0; i < len; i++) { printf("%02X ", str[i]); if (((i % BYTES_PER_LINE) = = (BYTES_PER_LINE - 1)) && ((i + 1) != len)) { putchar('\n'); for (j = 0; j < preflen; j++) putchar(' '); } else if ((i % BYTES_PER_GROUP) = = (BYTES_PER_GROUP - 1)) putchar(' '); } putchar('\n'); }
This function takes the following arguments:
-
prefix
String to be printed in front of the hexadecimal output. Subsequent lines of output are indented appropriately.
-
str
String to be printed, in binary. It is represented as an
unsigned char *
to make the code simpler. The caller will probably want to cast, or it can be easily rewritten to be avoid *
, which would require this code to cast this argument to a byte-based type for the array indexing to work correctly.-
len
Number of bytes to print.
This function prints out bytes as two characters, and it pairs bytes in groups of four. It will also print only 16 bytes per line. Modifying the appropriate preprocessor declarations at the top easily changes those parameters.
Currently, this function writes to the standard output, but it can be
modified to return a malloc( )
‘d
string quite easily using sprintf( )
and
putc( )
instead of printf( )
and putchar( )
.
In C++, you can print any data object in hexadecimal by setting the
flag ios::hex
using the setf( )
method on ostream
objects (the unsetf(
)
method can be used to clear flags). You might also want
the values to print in all uppercase, in which case you should set
the ios::uppercase
flag. If you want a leading
“0x” to print to denote
hexadecimal, also set the flag ios::showbase
. For
example:
cout.setf(ios::hex | ios::uppercase | ios::showbase); cout << 1234 << endl; cout.unsetf(ios::hex | ios::uppercase | ios::showbase);
Get Secure Programming Cookbook for C and C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.