Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Convert between "base-32" and ASCII
- # Written for python 3, but seems to work in python 2 as well
- from sys import argv
- def int2base(x,b,alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
- # Original function courtesy of http://stackoverflow.com/a/2267721/1175714
- # Most of the function is omitted; assuming x is non-negative real, as that's what an ASCII char is.
- rets=''
- while x>0:
- x,idx = divmod(x,b)
- rets = alphabet[idx] + rets
- return rets
- def b32_ascii(src):
- a = src
- a2 = "%x" % int(a,32)
- b = ''
- i = 0
- while 1:
- if i >= len(a2): break
- b = b + chr(int(a2[i] + a2[i+1],16))
- i += 2
- print(b)
- def ascii_b32(src):
- a = src
- b = ''
- for i in a:
- b = b + int2base(ord(i),16)
- print(int2base(int(b,16),32))
- if len(argv) >= 2:
- if argv[1] == "b":
- b32_ascii(argv[2])
- elif argv[1] == "a":
- ascii_b32(argv[2])
- else:
- print("=========================================================")
- print("Usage:\t\tpython %s [option] [string]" % argv[0])
- print("Options:")
- print(" b:\t\tconvert base-32 string to ASCII")
- print(" a:\t\tconvert ASCII string to base-32")
- print("=========================================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement