An easy question: how do you print a long long nicely as hex?

I wanted to print a long long nicely, consistent with other lines of output. It took me a while to get working properly.

The code

printf("... %20.16llx\n",ll);
  • The x means print as hexadecimal
  • ll means treat the value as a long long
  • 20.16. The 20 is the the minimum number of characters printed. The second 16 specifies the number of characters to be output. In the past, I’ve used formatting to print numbers so they line up in a column.

Below is the formatting string, and the output

.%16.llx.    .              68.
.%16.16llx. .0000000000000068.
.%.llx. .68.
.%llx. .68.

My final formatting string is

printf("Serial Number in hex:%llx\n",ll);

Serial Number in hex:68

It is another of the “it is always easy when you know the answer”.

My original problem was “how do you nicely print a variable length string”.

I solved it

char longChar[8];
char * pData; // points to the data
int lData = ... ; // length of the data
memset(&longChar[0],0,sizeof(longChar); // clear it to 0
memcpy(&longChar[8-lData],
pdata,
lData
);
long long ll;
memcpy(&ll,&longChar,8);
printf("Serial number Hex %llx\n",ll);

There may be better ways of doing it (please suggest a better way of doing it), but it works.

2 thoughts on “An easy question: how do you print a long long nicely as hex?

  1. I also used to struggle with printing hex chars. Then one day, I came to the realization that the %x format specifier is only for integer (type) values, obviously long long is part of that support. But, it’s still sometimes tricky. And limited since it’s only numbers.

    So, I pondered over it a while, knowing that there were lots of times I needed to print out non-numeric data in hex also, so I ended up writing a generic hex-to-string function. I’ve seen many flavors of this type of function over the years, but I wanted something concise that didn’t have to loop through every character to get the job done.

    I ended up implementing a xtos() (hex to string) function. The prototype is

    char * xtos(char * output_buffer, void * input_data, unsigned length_of_data) ;

    The key to this function is using the builtin.h __trot() function. Translate one to two. It requires a translate table of 256 two-byte (short data type) entries. The 0th translate sequence for input character for X’00’ is X’F0F0′. The 255th translate sequence for input character x’FF’ is X’C6C6′. You can probably fill in the rest. The first parameter is the buffer where the printable hex will go, and the return value is simply a pointer to that buffer. The answer is null terminated, thus the “+1” that’s needed when declaring the output_buffer length.

    The conversion is performed in one TROT hardware instruction of up to 2GB in length. Here’s an example of using it:

    long long mydata = 0x12345678ABCDEF01 ;
    char outbuff[sizeof(mydata)*2+1] ;

    printf(“mydata (%lld) in hex is >%s<\n”, mydata, xtos(outbuff, &mydata, sizeof(mydata)) ;

    This would be quite useful in a dump formatting routine for a chunk of storage or a control block.

    Like

    1. Hi Todd,
      Thanks for your interesting suggestion. I haven’t seen this before.

      Another solution I’ve seen is to go along each character. Take the lower nibble and look it up in ‘0123…F’ then, then do the upper nibble.
      But I would also need some logic to ignore leading 00s.

      I have a printhex routine (out in one of my githubs) which I use for dumps.

      Like

Leave a comment