Skip to content

Instantly share code, notes, and snippets.

@tmthydvnprt
Last active December 10, 2016 11:59
Show Gist options
  • Select an option

  • Save tmthydvnprt/db557be31fcb28b6b784a3d698f377be to your computer and use it in GitHub Desktop.

Select an option

Save tmthydvnprt/db557be31fcb28b6b784a3d698f377be to your computer and use it in GitHub Desktop.
ASCII <-> Binary <-> Hex
def string2bitstring(s=''):
return ''.join([bin(ord(x))[2:].zfill(8) for x in s])
def bitstring2string(b=None):
return ''.join([chr(int(x, 2)) for x in [b[i:i+8] for i in xrange(0, len(b), 8)]])
def string2bitlist(s=''):
return [bin(ord(x))[2:].zfill(8) for x in s]
def bitlist2string(b=None):
return ''.join([chr(int(x, 2)) for x in b])
def string2hexstring(s=''):
return ''.join([hex(ord(x))[2:].zfill(2) for x in s])
def string2hexlist(s=''):
return [hex(ord(x))[2:].zfill(2) for x in s]
def hexstring2string(b=None):
return ''.join([chr(int(x, 16)) for x in [b[i:i+2] for i in xrange(0, len(b), 2)]])
def hexlist2string(b=None):
return ''.join([chr(int(x, 16)) for x in b])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment