Hex byte string int coversion

Reference:

# Python Types Coversion

hey, there are some usefull features below, I will update at intervals:

bit_length() New in version 3.1:

In [73]: n = -37

In [74]: bin(n)
Out[74]: '-0b100101'

In [75]: n.bit_length()
Out[75]: 6

hex to int:

In [59]: int('0x22357', 16)
Out[59]: 140119

int to hex:

In [32]: hex(141128)
Out[32]: '0x22748'

packed binary data to int:

In [70]: struct.unpack("I", b'\x57\x23\x02\x00')
Out[70]: (140119,)

In [71]: type(struct.unpack("I", b'\x57\x23\x02\x00'))
Out[71]: tuple

In [72]: struct.unpack("I", b'\x57\x23\x02\x00')[0]
Out[72]: 140119

int to packed binary data:

In [91]: struct.pack('I', 140119)
Out[91]: b'W#\x02\x00'

In [92]: struct.pack('I', 140119).hex()
Out[92]: '57230200'
0%