python int to bytes code example
Example 1: python convert string to bytes
data = ""
data = "".encode()
data = b""
data = b"".decode()
data = str(b"")
Example 2: 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'
Example 3: int to byte array python
>>> bytes([2])
b'\x02`