0

I am reading four bytes from file I would like to join them

g = f.read(60)
f.seek (60)
k60 =f.read(1)
print('byte60',k60)
k61 =f.read(1)
print('byte61',k61)
k62 =f.read(1)
print('byte62',k62)
k63 =f.read(1)
print('byte63',k63)
print(k63,k62,k61,k60)
print (b''.join([k63,k62,k61,k60]))

Result is:

b'\x00\x00\x00\x80'

I would like to receive:

00000080
Remi Guan
  • 21,506
  • 17
  • 64
  • 87

1 Answers1

0

You to convert a byte string to its hex representation, you can use the hexlify() method from the binascii module:

>>> from binascii import hexlify
>>> ...
>>> raw = b''.join([k63,k62,k61,k60])
>>> print(hexlify(raw))
b'00000080'
>>> print(hexlify(raw).decode('ascii')  # if you want to convert it to a string
00000080

The same could be accomplished by using codecs.encode(raw, 'hex').

mata
  • 67,110
  • 10
  • 163
  • 162