Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unsigned long long type printing in hexadecimal format

Tags:

c

integer

printf

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.

like image 751
Paul the Pirate Avatar asked Oct 10 '13 00:10

Paul the Pirate


People also ask

How do I print unsigned long?

%ul will just print unsigned (with %u), and then the letter "l" verbatim. Just as "%uw" will print unsigned, followed by letter "w".

How do you print a number in hexadecimal?

To print integer number in Hexadecimal format, "%x" or "%X" is used as format specifier in printf() statement. "%x" prints the value in Hexadecimal format with alphabets in lowercase (a-f). "%X" prints the value in Hexadecimal format with alphabets in uppercase (A-F).

What is a long hex?

long hexLong = 0XABL; For Hexadecimal, the 0x or 0X is to be placed in the beginning of a number. Note − Digits 10 to 15 are represented by a to f (A to F) in Hexadecimal.

How do I print an unsigned number?

An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables. printf(“%u”, value);


3 Answers

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:

  • printf documentation on cppeference.com
like image 87
gavinb Avatar answered Oct 06 '22 08:10

gavinb


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

%llx prints long long unsigned in hex

like image 6
Iłya Bursov Avatar answered Oct 06 '22 08:10

Iłya Bursov


printf("Hex add is: %llu", hexAdd);
like image 2
Joseph Avatar answered Oct 06 '22 10:10

Joseph