python bytes() code example
Example 1: Python bytes
>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (1024).to_bytes(10, byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
b'\xe8\x03'
Example 2: Python bytes
def bit_length(self):
s = bin(self)
s = s.lstrip('-0b')
return len(s)
Example 3: join python documentation
>>> ''.join(['A', 'B', 'C'])
'ABC'
>>> ''.join({'A': 0, 'B': 0, 'C': 0})
'ACB'
>>> '-'.join(['A', 'B', 'C'])
'A-B-C'