Created
May 11, 2017 12:02
-
-
Save pardo/bd9969c543e82472bbd9153729267a0a to your computer and use it in GitHub Desktop.
encode number as string
This file contains 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
import base64 | |
def encode(number): | |
out = "" | |
while number > 0: | |
first_byte = number & 0b11111111 | |
out = chr(first_byte) + out | |
number = number >> 8 | |
return out | |
def decode(string_number): | |
n = 0 | |
l = len(string_number) | |
for i, c in enumerate(string_number, 1): | |
n = n | ord(c) | |
if l > i: | |
n = n << 8 | |
return n | |
encoded_number = base64.encodestring(encode(0xfacd1000)) | |
print(encoded_number) | |
decoded_number = decode(base64.decodestring(encoded_number)) | |
assert decoded_number == 0xfacd1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment