Created
August 25, 2016 08:42
-
-
Save Mego/af2d00f037046e19d5d3d22076d5c209 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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