Skip to content

Instantly share code, notes, and snippets.

@xeoron
Forked from agfor/python_conversion.md
Created April 20, 2013 17:30
Show Gist options
  • Save xeoron/5426746 to your computer and use it in GitHub Desktop.
Save xeoron/5426746 to your computer and use it in GitHub Desktop.

Python Hex

TO
FROM   Example Integer Integer String ASCII Binary String ASCII Hex String Binary String ASCII Hex of Integer String
Example   45 "45" "101101" "2D" "x00x00x00x2d" "3435"
Integer 45
str(data) bin(data)* hex(data)* struct.pack('!i', data)**
Integer String "45" int(data)
T T struct.pack('!i', int(data))
ASCII Binary String "101101" int(data, 2) T
T T
ASCII Hex String "2D" int(data, 16) T T
binascii.unhexlify(data) data.decode('hex')
Binary String "x00x00x00x2d" struct.unpack('!i', data)[0] T T binascii.hexlify(data)
ASCII Hex of Integer String "3435"
data.encode('hex')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment