Skip to content

Instantly share code, notes, and snippets.

@pardo
Created May 11, 2017 12:02
Show Gist options
  • Save pardo/bd9969c543e82472bbd9153729267a0a to your computer and use it in GitHub Desktop.
Save pardo/bd9969c543e82472bbd9153729267a0a to your computer and use it in GitHub Desktop.
encode number as string
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