Byte to integer ? why so complicated in python?

Hi,

does anyone know how to read a byte from a binary file and convert to an integer

I been busy for one hour on this, while it should be so simple, I keep running into errors after errors

f = open(filename, "rb")
a = bytearray(b'\x00')
a.append(f.read(1)) <<<<<<<<<<<<<<<<<<<<<<<< error
nameLen = int.from_bytes(bytes(a))
'bytes' object cannot be interpreted as an integer

I never thought this could be so complicated in python

thanks for your help

int.from_bytes(b, byteorder='big', signed=False)

or,

def bytes_to_int(bytes):
  return int(bytes.encode('hex'), 16)

So, this works:

f = open(filename, "rb")
a = f.read()
nameLen = int.from_bytes(a, byteorder='big', signed=False)

thanks it works !!

1 Like