3

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!

7
  • 4
    Are you asking about why it keeps reading past the null byte, or why it only prints 0 instead of 00? (It should be printing 0x in front of all these hex representations, and it should be printing them on separate lines; it looks like you edited the output. Please don't do that.) Commented May 18, 2017 at 22:26
  • 1
    What do you mean by "goes wrong"? It doesn't print what you want? It doesn't break when you want it to? What to you want to happen? Commented May 18, 2017 at 22:29
  • You should get a similar result with any byte with a leading zero. Commented May 18, 2017 at 22:33
  • 1
    -Since I stated before, I'm reading a binary file , so, no, I'm obviously not asking why it keeps reading past the null byte. -Though this is not as obvious, since I'm reading binary data, I'm not concerned why the print() prints 0x0 instead of 0x00. -What I'm concerned about is why it reads 0x31 instead of 0x4a Commented May 19, 2017 at 7:42
  • 1
    certainly, you may: b'\xff\xd8\xff\xe0\x00\x10130y' That's my issue: dispite the fact I want to read byte by byte, I don't understand why it's reading as if it was utf-8. I guess I have to specify a certain encoding to the open(), but I dunno which one. Commented May 19, 2017 at 11:51

2 Answers 2

2

I think the issue is you are trying to dereference current_byte as though it were an array, but it is simply a byte

def test_write_bin(self):  
  fname = 'test.bin'
  f = open(fname, 'wb')
  # ff d8 ff e0 00 10 4a
  f.write(bytearray([255, 216, 255, 224, 0, 16, 74]))
  f.close()
  with open(fname, "rb") as f2:
    while True:
      current_byte = f2.read(1)
      if (not current_byte):
        break
      val = ord(current_byte)
      print hex(val),
  print

This program produces the output:

0xff 0xd8 0xff 0xe0 0x0 0x10 0x4a
Sign up to request clarification or add additional context in comments.

Comments

0
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

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.