3

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
  • 1
    What is x = 16? I don't understand what you're doing here. Commented Dec 29, 2018 at 9:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.