Created
January 16, 2014 06:30
-
-
Save debbbbie/8450662 to your computer and use it in GitHub Desktop.
64进制互转
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
#coding=gbk | |
digit64 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/' | |
def int_to_str64( x ): | |
try: | |
x=int(x) | |
except: | |
x=0 | |
if x<0: | |
x=-x | |
if x==0: | |
return "0" | |
s="" | |
while x>64: | |
x1= x % 64 | |
s = digit64[x1]+s | |
x = x // 64 | |
if x>0: | |
s = digit64[x]+s | |
return s | |
def str64_to_int( s ): | |
x = 0 | |
s = str(s).strip() | |
if s=="": | |
return x | |
for y in s: | |
k = digit64.find(y) | |
if k>=0: | |
x=x*64+k | |
return x | |
x = 58 | |
s = int_to_str64(x) | |
y = str64_to_int( s) | |
print( x , s , y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment