Python - Cannot read hexadecimal 0A correctly

Viewed 195

I'm trying to read 2 bytes at a time from a file in hexadecimal.

f = open(filename, "rb+")


seekv = 0
x = 16
while x > 0:
     x = x-1
     f.seek(seekv)
     seekv = seekv + 1
     Nextb = binascii.hexlify(f.readline(2))
     print Nextb

Problem is, if those 2 bytes are 0a0a, it will only read one byte, 0a. I suspect that has something to do with 0x0A being New Line in ASCII, but that shouldn't happen.

1 Answers

You are using f.readline(2) instead of f.read(2)

readline is going to use one of those 0a as a marker to know where a new line starts. If you switch to f.read(2), you should see both.

You really shouldn't use readline with binary data.

Related