aboutsummaryrefslogtreecommitdiff
path: root/final/rsautils.py
blob: 236571ca268553f1989bf25fccd6707626a3c2b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def list2int(arr,base,endian="little"):
  if endian == "big":
    arr.reverse()
  out = 0
  for num in arr:
    out *= base
    out += num
  return out

def int2list(num,base,endian="little"):
  arr = []
  while num > 0:
    arr.append(num%base)
    num = num//base
  if endian == "little":
    arr.reverse()
  return arr

def int2string(num):
  return ''.join([chr(charval) for charval in int2list(num,128)])

def string2int(string,endian="little"):
  return list2int([ord(char) for char in string],128,endian=endian)

def int2base64(num):
  return ''.join([chr(charval+32) for charval in int2list(num,64)])

def base642int(code):
  return list2int([ord(char)-32 for char in code],64)