Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python read binary file
#1
as topics. i want to read a binary file. i need some some offset in it and control by big or little endian. but i have problem with it.

path_xd = 'xxxxx'
pattern = b'\x14\x02'
reg_xd=re.compile(pattern)
with open(path_xd, 'r+b') as p_xd:
    data=p_xd.read()
    for x in reg_xd.finditer(data):
        offset=x.start()
        p_xd.seek(offset)
        data_1402=p_xd.read(16)
        xd_name=int.from_bytes(data_1402[2:4], byteorder='little')
        xd_name=(str(xd_name).replace('0', '')).zfill(2)
        print(xd_name)
        print(data_1402)
        print(data_1402[5:9])
i get output like this
Output:
b'\x14\x02\x01\x00 \x00\x00\x00\x03\x18\x90BW#\x08\x00'
it's how it look like in termial
Output:
402 0100 2000 0000 0318 9042 5723 [..... ......BW# 00000040: 0800 5723 0800
it seems missing some bytes in python output.

i want result like this format. for fullfile. seems i have misunderstand something here. need help with file hex output and big/litte endian please advice.thanks.
Output:
\x14\x02\x01\x00\x20\x00\x00\x00\x03\x18\x90\x42\x57\x23\x08\x00\x57\x23\x08\x00
Reply
#2
up. anyone can help? thanks!
Reply
#3
I don't understand when your output appears. The first one says you get "output like that", and the second one says it's how it looks in the terminal. I would expect your output to be in a terminal. How do you produce the first and how do you produce the second?

Do you have an example of the 'xxxxx' file that someone could execute your code against?
Reply
#4
Looks like at least partly you have a byte string. When printed by default, if the byte maps to an ASCII character (like \x20), then it prints that character. \x20 is a space, so the 20s are removed and replaced with spaces in your printout.

If you just need the hex values, you could use (among other things) the hex method.

>>> b = b'\x14\x02\x01\x00\x20\x00\x00\x00\x03\x18\x90\x42\x57\x23\x08\x00\x57\x23\x08\x00'
>>> b
b'\x14\x02\x01\x00 \x00\x00\x00\x03\x18\x90BW#\x08\x00W#\x08\x00'
>>> b.hex()
'1402010020000000031890425723080057230800'
If you want the specific format, you could split it up and add the "\x" in front.

>>> h = b.hex()
>>> print("".join([f"\\x{h[i:i+2]}" for i,_ in enumerate(h[::2])]))
\x14\x40\x02\x20\x01\x10\x00\x02\x20\x00\x00\x00\x00\x00\x00\x00\x03\x31\x18\x89
Reply
#5
thanks for your help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 986 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,200 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,527 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,508 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,153 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,559 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,727 Nov-09-2023, 10:56 AM
Last Post: mg24
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 1,730 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  read file txt on my pc to telegram bot api Tupa 0 2,525 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,216 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020