Python: Number Conversion Chart

This post is a few years old now, so some details (or my opinions) might be out of date.
I would still love to hear your feedback in the comments below. Enjoy!

Ever forget how to take a hex string like "2D" and convert it into binary data? Or how to parse an ascii-binary string like "101101" into a plain integer? Me too.

That’s why I crafted this table. It doens’t include all conversion, but it includes every conversion type I know, and you can chain them to get from any source type to any destination.

Every from/to cell assumes that the “original” integer is 45. Enjoy!

View this Gist on GitHub

FromToExpression
45"45"str(data)
45"101101"bin(data)
45"2D"hex(data)
45"\x00\x00\x00\x2d"struct.pack('!i', data)
"45"45int(data)
"45""3435"data.encode('hex')
"101101"45int(data, 2)
"2D"45int(data, 16)
"2D""\x2d"binascii.unhexlify(data) or data.decode('hex')
"\x00\x00\x00\x2d"45struct.unpack('!i', data)[0]
"\x2d""2D"binascii.hexlify(data)
"3435""45"data.decode('hex')
Discuss this post at the comment section below.
Follow me on Twitter, Facebook or Google+

Similar Posts