Skip to content

Instantly share code, notes, and snippets.

@najeira
Created November 5, 2011 09:02
Show Gist options
  • Save najeira/1341295 to your computer and use it in GitHub Desktop.
Save najeira/1341295 to your computer and use it in GitHub Desktop.
encode and decode between int,long and string by Python
import struct, base64
def long_to_str(value):
value = struct.pack('q', value)
value = base64.urlsafe_b64encode(value)
value = value.rstrip('=')
value = value.rstrip('A')
return value
def str_to_long(value):
num_a = 11 - len(value)
if num_a:
value = value + ('A' * num_a)
value = value + '='
value = base64.urlsafe_b64decode(value)
return struct.unpack('q', value)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment