to bytes python

Solutions on MaxInterview for to bytes python by the best coders in the world

showing results for - "to bytes python"
Isaac
22 Feb 2017
1>>> (1024).to_bytes(2, byteorder='big')
2b'\x04\x00'
3>>> (1024).to_bytes(10, byteorder='big')
4b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
5>>> (-1024).to_bytes(10, byteorder='big', signed=True)
6b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
7>>> x = 1000
8>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
9b'\xe8\x03'
10