4.3 Representing Binary Keys (or Other Raw Data) as Hexadecimal

4.3.1 Problem

You want to print out keys in hexadecimal format, either for debugging or for easy communication.

4.3.2 Solution

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.

4.3.3 Discussion

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 
/* Don't change these */
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)) {
      for (j = 0;  j < preflen;  j++) putchar(' ');
    else if ((i % BYTES_PER_GROUP) =  = (BYTES_PER_GROUP - 1)) putchar(' ');

This function takes the following arguments:


String to be printed in front of the hexadecimal output. Subsequent lines of output are indented appropriately.


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 a void *, which would require this code to cast this argument to a byte-based type for the array indexing to work correctly.


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);