python read binary file byte by byte

Viewed 8998

This might seem pretty stupid, but I'm a complete newbie in python.

So, I have a binary file that starts by

ff d8 ff e0 00 10 4a

(as seen both through Hex Editor Neo and a java program)

yet, when I tried to read it with python by

with open(absolutePathInput, "rb") as f:
    while True:
        current_byte = f.read(1)
        if(not current_byte):
            break
        print(hex(current_byte[0]))

I get

ff d8 ff e0 0 10 31

It seems that it goes wrong whenever the first 0x00 is readen.

what am I doing wrong? Thank u!

2 Answers
import binascii

with open(absolutePathInput, "rb") as f:
    buff = f.read()

line = binascii.hexlify(buff)
hex_string = [line[i:i+2] for i in range(0, len(line), 2)]

simple and violent

Related