Skip to content

Instantly share code, notes, and snippets.

@Mego
Created August 25, 2016 08:42
Show Gist options
  • Save Mego/af2d00f037046e19d5d3d22076d5c209 to your computer and use it in GitHub Desktop.
Save Mego/af2d00f037046e19d5d3d22076d5c209 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def pack(s):
binary_string = ''
for c in s:
binary_string += '{:07b}'.format(ord(c))
binary_string += '0'*((8 - len(binary_string)) % 8)
byte_string = ''
for i in range(0, len(binary_string), 8):
byte_string += chr(int(binary_string[i:i+8], 2))
return byte_string
def unpack(s):
binary_string = ''
for c in s:
binary_string += '{:08b}'.format(ord(c))
binary_string = binary_string[:-(len(binary_string) % 7)]
original_string = ''
for i in range(0, len(binary_string), 7):
original_string += chr(int(binary_string[i:i+7], 2))
return original_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment