Skip to content

Instantly share code, notes, and snippets.

@TimSC
Created December 5, 2014 13:54
Show Gist options
  • Save TimSC/bed3b068d241f4abe167 to your computer and use it in GitHub Desktop.
Save TimSC/bed3b068d241f4abe167 to your computer and use it in GitHub Desktop.
Convert binary data to hex representation and back
import string
def BinToHex(a):
return "".join(["%02x" % ord(ch) for ch in a])
def HexToBin(a):
out = []
for i in range(0, len(a), 2):
twoChrs = a[i:i+2]
v = string.atoi(twoChrs, 16)
out.append(chr(v))
return "".join(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment