Created
November 5, 2011 09:02
-
-
Save najeira/1341295 to your computer and use it in GitHub Desktop.
encode and decode between int,long and string by Python
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 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