unsigned long long type printing in hexadecimal format

100,305

Solution 1

You can use the same ll size modifier for %x, thus:

#include <stdio.h>

int main() {
    unsigned long long x = 123456789012345ULL;
    printf("%llx\n", x);
    return 0;
}

The full range of conversion and formatting specifiers is in a great table here:

Solution 2

try %llu - this will be long long unsigned in decimal form

%llx prints long long unsigned in hex

Solution 3

printf("Hex add is: %llu", hexAdd);

Solution 4

I had a similar issue with this using the MinGW libraries. I couldn't get it to recognize the %llu or %llx stuff.

Here is my answer...

void PutValue64(uint64_t value) {
char    a_string[25];   // 16 for the hex data, 2 for the 0x, 1 for the term, and some spare
uint32  MSB_part;
uint32  LSB_part;

    MSB_part = value >> 32;
    LSB_part= value & 0x00000000FFFFFFFF;

    printf(a_string, "0x%04x%08x", MSB_part, LSB_part);
}

Note, I think the %04x can simply be %x, but the %08x is required.

Good luck. Mark

Share:
100,305
Paul the Pirate
Author by

Paul the Pirate

Updated on July 13, 2022

Comments

  • Paul the Pirate
    Paul the Pirate almost 2 years

    I am trying to print out an unsigned long long like this:

      printf("Hex add is: 0x%ux ", hexAdd);
    

    but I am getting type conversion errors since I have an unsigned long long.

  • Paul the Pirate
    Paul the Pirate over 10 years
    But I want the hex version of it, hence the reason for using x in the first place.
  • Joseph
    Joseph over 10 years
    In that case, use %llx but I'm assuming by your second comment you figured that out.
  • sdaau
    sdaau about 10 years
    But printf("%llu\n", (long long unsigned int) 243); prints 243, which is decimal, not hex; printf("%llx\n", (long long unsigned int) 243); prints f3 in hex.