Search code examples
c++binary64-bitreverse-engineeringhexdump

Output 64 bit integer with hexdump


I currently work on some binary data. In order to check and debug the data previously generated by my application I use hexdump, facing the obstacle of hexdump not appearing to be able to extract a 64-bit integer field. Given the following minimal example:

#include <iostream>
#include <fstream>
#include <cstdint>

int main(int argc, char** argv){
    std::ofstream os("tmp.bin", std::ios::out | std::ios::binary);
    uint64_t x = 7;
    os.write((char*)&x,sizeof(uint64_t));
    os.close();
    return 0;
}

I perform a simple hexdump on my system:

hexdump tmp.bin

> 0000000: 0007 0000 0000 0000
> 0000008:

Now trying to extract the unsigned int of 64 bit width yields:

hexdump -e '/8 "%u"' tmp.bin

> hexdump: bad byte count for conversion character u

According to the well written hexdump-manual by David Mair it should be possible to do it, but I have no success.

What am I missing ?


Solution

  • Our final workaround reads as follows:

    x=`hexdump -n 8 -e '2/4 "%08X " "\n"' {FILENAME} | awk 'BEGIN{printf "0x"}{print $2$1}'`
    echo $(($x))
    

    Explaination for each part:

    • Extract the eight bytes of the 64-bit integer value from file {FILENAME} as two four byte chunks printed as hexadecimal encoded values.

      hexdump -n 8 -e '2/4 "%08X " "\n"' {FILENAME}
      
    • Reverses the byte order of the two chunks and prints it as a single eight byte chunk representing the binary value. Prepend 0x for the following processing.

      awk 'BEGIN{printf "0x"}{print $2$1}
      
    • Save the hexadecimal representation into x for bash to evaluate.

      x=`....`
      
    • Let the bourne shell interpret and output the hexadecimal encoded value of the 64-bit integer variable (here the previously prepended 0x is needed).

      echo $(($x))