int.to_bytes vs bytes() code example

Example 1: 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 2: join python documentation

>>> ''.join(['A', 'B', 'C'])
'ABC'
>>> ''.join({'A': 0, 'B': 0, 'C': 0}) # note that dicts are unordered
'ACB'
>>> '-'.join(['A', 'B', 'C'])  # '-' string is the seprator
'A-B-C'