Skip to content

Instantly share code, notes, and snippets.

@saml
Created June 10, 2011 11:42
Show Gist options
  • Save saml/1018674 to your computer and use it in GitHub Desktop.
Save saml/1018674 to your computer and use it in GitHub Desktop.
import base64
import sys
tb_hex_bin = {
0x0: '0000',
0x1: '0001',
0x2: '0010',
0x3: '0011',
0x4: '0100',
0x5: '0101',
0x6: '0110',
0x7: '0111',
0x8: '1000',
0x9: '1001',
0xa: '1010',
0xb: '1011',
0xc: '1100',
0xd: '1101',
0xe: '1110',
0xf: '1111'
}
tb_bin_hex = {}
for k,v in tb_hex_bin.iteritems():
tb_bin_hex[v] = k
def to_hex(s):
return [hex(ord(x)) for x in base64.b64decode(s, '*-')]
def pr_hex(s, newline_every=10000):
i = 0
for x in (ord(x) for x in base64.b64decode(s, '*-')):
sys.stdout.write(tb_hex_bin[x>>4])
sys.stdout.write(tb_hex_bin[(x<<4)>>8])
i = i + 1
if i == newline_every:
i = 0
print('')
def pr_tune(binary_str):
chunk_size = 4
i = 0
ords = []
n = len(binary_str)
while True:
if i > n - (chunk_size * 2):
break
first_hex = binary_str[i:i+chunk_size]
second_hex = binary_str[i+chunk_size:i+chunk_size+chunk_size]
x = (tb_bin_hex[first_hex] << chunk_size) + tb_bin_hex[second_hex]
print(first_hex,second_hex,x)
ords.append(x)
i = i + chunk_size + chunk_size
print(base64.b64encode(''.join(chr(x) for x in ords), '*-'))
def to_int(s):
if s.endswith('*'):
s = s + '=='
return [ord(x) for x in base64.decodestring(s)]
def g(s):
return base64.encodestring(''.join([chr(int(x,16)) for x in s]))
def to_tune(*hexs):
return base64.b64encode(''.join(chr(x) for x in hexs), '*-')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment