Skip to content

Instantly share code, notes, and snippets.

@jceloria
Forked from agfor/python_conversion.md
Created May 25, 2018 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jceloria/312ae86b9ea1090a2af9d2e50626fba9 to your computer and use it in GitHub Desktop.
Save jceloria/312ae86b9ea1090a2af9d2e50626fba9 to your computer and use it in GitHub Desktop.

Python Number Conversion Chart

From To Expression
45 "45" str(data)
45 "101101" bin(data)[2:] or '{0:b}'.format(data)
45 "0x2D" hex(data)[2:] or '{0:x}'.format(data)
45 "\x00\x00\x00\x2d" struct.pack('!i', data)
"45" 45 int(data)
"45" "3435" data.encode('hex') / codecs.encode(b"45", "hex_codec") in Python 3
"101101" 45 int(data, 2)
"2D" 45 int(data, 16)
"2D" "\x2d" binascii.unhexlify(data) or data.decode('hex') (Removed in Python 3)
"\x00\x00\x00\x2d" 45 struct.unpack('!i', data)[0]
"\x2d" "2D" binascii.hexlify(data)
"3435" "45" data.decode('hex') / codecs.decode(b"3435", "hex_codec") in Python 3

Comments are welcome here or in the original blog post regarding this table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment