How to printf a 64-bit integer as hex?

sergej

With the following code I am trying to output the value of a unit64_t variable using printf(). Compiling the code with gcc, returns the following warning:

warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat=]

The code:

#include <stdio.h>
#include <stdint.h>

int main ()
{
    uint64_t val = 0x1234567890abcdef;
    printf("val = 0x%x\n", val);

    return 0;
}

The output:

val = 0x90abcdef

Expected output:

val = 0x1234567890abcdef

How can I output a 64bit value as a hexadecimal integer using printf()? The x specifier seems to be wrong in this case.

tangrs

The warning from your compiler is telling you that your format specifier doesn't match the data type you're passing to it.

Try using %lx or %llx. For more portability, include inttypes.h and use the PRIx64 macro.

For example: printf("val = 0x%" PRIx64 "\n", val); (note that it's string concatenation)

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to determine the 64-bit signed integer type and printf specifier with CMake?

How to create a 64 bit Unique Integer in Java

How big can a 64 bit unsigned integer be?

How to convert 8-bit integer to Hex color value?

How to convert integer to unsigned 64-bit integer

How to convert 64 bit hex value to double in c?

LUA 5.4 - How to convert 64-bit numbers to hex

<SOLVED> Converting hex string to a 64-bit integer/timestamp using R

How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer?

How to turn a double to a 64-bit integer bit by bit using unions

How to map all 64-bit integers to a different 64-bit integer in a 1-to-1 fashion

How to get a 64-bit integer back from Presto in R?

How to generate random 64-bit unsigned integer in C

How to represent 64-bit integer in VB6?

How to check availability of 64-bit integers (for `use integer`)?

How should a 64-bit integer literal be represented in C++?

How to convert and Integer to an 64-bit byte representation in python

Format comma for a bit 64 integer

Output 64 bit integer with hexdump

How to add 2^63 to a signed 64-bit integer and cast it to a unsigned 64-bit integer without using 128-bit integer in the middle

Convert Hex to signed 64-bit with bash

64bit Hex to Decimal in Javascript

NASM printf print 64 bit double segfaults

Portable printf format specifier for 64 bit hexadecimal?

How does Rust's 128-bit integer `i128` work on a 64-bit system?

How to multiply double (floating point) by an integer type (32-bit, 64-bit, 128-bit, etc.) manually

bash convert hex string to integer code using printf failing

How to convert a hex string to an integer?

Fixed 64 bit integer in Linux Kernel