Aisha
Aisha

Reputation: 127

How to read binary data and print in binary or hexadecimal format?

I am working with binary data. I have a file containing 2KB of binary data. I have used the following code to read the file and then print it. I have also tried to view the file contents using hexdump in the terminal. I am getting different outputs 1 and 2 (shown in attached screenshots) of the same file in python and hexdump. I am assuming it might be due to the encoding scheme used by python? I am very naive about working with binary data. Can anyone kindly check it and let me know the reasons for this? I also want to know if that's the correct way of reading a large binary file?

print("First File \n");
f1 = open("/data/SRAMDUMP/dataFiles/first.bin","rb")
num1 = list(f1.read())
print(num1)
f1.close()

PythonOutput Hexdump

Upvotes: 0

Views: 2924

Answers (1)

Masklinn
Masklinn

Reputation: 42227

I am assuming it might be due to the encoding scheme used by python?

There is no "encoding scheme" hexdump formats binary data as hex (two nibbles per byte), you've converted the binary contents of the file to a list which yields a list of integers (since that's what bytes are in python).

If you want to convert bytes to printable hex in Python, use the bytes.hex method. If you want something similar to hexdump, you'll need to take care of slicing, spacing and carriage-return-ing.

A slow version would simply read the file 2 bytes per 2 bytes, hex them, print them, then linebreak every 16 bytes. Python 3.8 adds formatting facilities to bytes.hex() meaning you can even more easily read bytes 16 by 16 with a separator every two, though that doesn't exactly match hexdump's format:

f = open(sys.argv[1], 'rb')
it = iter(functools.partial(f.read, 16), '')

for i, b in enumerate(it):
    print(f'{16*i:07x} {b.hex(" ", 2)}')

Also beware that hexdump follows the platform's endianness by default which is... rarely what you want, and will not match your Python output. hexdump -C prints bytes in the file order.

Upvotes: 1

Related Questions