2

I am trying to read and convert binary into text that anyone could read. I am having trouble with the error message: 'utf-8' codec can't decode byte 0x81 in position 11: invalid start byte

I have gone throughout: Reading binary file and looping over each byte trying multiple versions of trying to open and read the binary file in some way. After reading about this error message, most people either had trouble with .cvs files, or had to change the utf-8 to -16. But reading up on https://en.wikipedia.org/wiki/UTF-16#Byte_order_encoding_schemes , Python does not use -16 anymore.

Also, if I add encoding = utf-16/32, the error states: binary mode doesn't take an encoding argument

Here is my code:

with open(b"P:\Projects\2018\1809-0068-R\Bin_Files\snap-pac-eb1-R10.0d.bin", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        byte = f.read(1)
print(f)

I am expecting to be able to read and write to the binary file. I would like to translate it to Hex and then to text (or to legible text somehow), but I think I have to go through this step before. If anyone could help with what I am missing, that would be greatly appreciated! Any way to open and read a binary file would be accepted. Thank you for your time!

Jake Kelly
  • 33
  • 3
  • Would you be able to share with us the binary file you are trying to read? – zabop Feb 05 '19 at 18:26
  • 1
    I don't know how to attach a file, but if you go to: https://www.opto22.com/support/resources-tools/downloads/snap_pac_firmware-zip you can download the .zip and unzip and it is the .bin file: ``` snap-pac-eb1-R10.0d.bin ``` – Jake Kelly Feb 05 '19 at 18:45
  • Thx. Do you know what is encoded in that file? (ie what we are supposed to get.) – zabop Feb 05 '19 at 19:24

1 Answers1

0

I am not sure but this might help:

import binascii

with open('snap-pac-eb1-R10.0d.bin', 'rb') as f:
    header = f.read(6)

b = bytearray(header)

binary=[bin(i)[2:].zfill(8) for i in b]

n = int('0b'+''.join(binary), 2)
nn = binascii.unhexlify('%x' % n)

nnn=nn.decode("ascii")[0:-1]

result='.'.join(str(ord(c)) for c in nnn[0:-1])

print(result)

Output:

16.0.8.0

zabop
  • 6,750
  • 3
  • 39
  • 84