convert int to byte python
pythonCopy>>> (258).to_bytes(2, byteorder="little") b'\x02\x01' >>> (258).to_bytes(2, byteorder="big") b'\x01\x02' >>> (258).to_bytes(4, byteorder="little", signed=True) b'\x02\x01\x00\x00' >>> (-258).to_bytes(4, byteorder="little", signed=True) b'\xfe\xfe\xff\xff'
Source: www.delftstack.com
python int to bytes
bytes = (-32757).to_bytes(2, 'little', signed = True) #store value in 2 bytes #bytes 2 int print(int.from_bytes([bytes[0], bytes[1]], 'little', signed=True)) #returns -32767
python int to byte
>>> bytes(10) b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Source: stackoverflow.com