Confirmed with Python 3.7.5.
b'\x00\x01\x02\x03'
# result: b'\x00\x01\x02\x03'
b'\x64\x65\x66\x67'
# result: b'defg' #Characters corresponding to ascii code are displayed
#without with
fp = open('filename.bin', 'rb')
all_bytes = fp.read()
fp.close()
#With with
with open('filename.bin', 'rb') as fp:
    all_bytes = fp.read()
a = 255  #Preparation code
a.to_bytes(2, 'little')  # to_bytes(Number of bytes after conversion,Endian)
# result: b'\xff\x00'
a = 255                         #Preparation code
byts = a.to_bytes(2, 'little')  #Preparation code
int.from_bytes(byts, 'little')  # int.from_bytes(bytes,Endian)
# result: 255
a = -255                                     #Preparation code
byts = a.to_bytes(2, 'little', signed=True)  #Preparation code
int.from_bytes(byts, 'little', signed=True)  # int.from_bytes(bytes,Endian, signed=True)
# result: -255
bytes.fromhex('F1E2f3f4')
bytes.fromhex('F1E2 f3f4')
bytes.fromhex('F1 E2 f3 f4')
# result: b'\xf1\xe2\xf3\xf4'
by = bytes.fromhex('F1E2f3f4')  #Preparation code
by.hex()
# result: 'f1e2f3f4'
Recommended Posts