如何将十进制转换为基数63

时间:2017-02-24 11:42:27

标签: python

这是我在网站上的第一个问题,给了足够的时间思考/搜索所以我可以找到答案,但我没有时间,我认为这是一个非常独特的问题值得问。这是我的代码:

alpha=list("0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
def encrypt(a):
    b=c=0
    for x in a[::-1]:
        b += (len(alpha)**c)*(alpha.index(x))
        c += 1
    return b
def decrypt(a):
    True
while True:         
    print(encrypt(input(">>> ")))

我的问题是它转换了基础63'很好,但我不知道如何将它转换回基数63,或纯文本。我想象答案与使用int()有关,但我真的无法解决它。

2 个答案:

答案 0 :(得分:0)

假设解密将被赋予int

def decrypt(a):
    remainder = a
    result = ""
    while remainder > 0:
        result = alpha[remainder % 63] + result
        remainder //= 63
    return result or "0" 

如果a是字符串,则更改remainder = int(a)

函数的第一行

答案 1 :(得分:0)

将十进制转换为基数n< 65 ## Heading ##的程序,我为python shell创建了这个算法,它可以帮助:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
def contri(num,Tx):
#ini Base:
    tr = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','y','z','#','@','*'];
    U = []
    p=0;
    alg = 10+3*(len(str(num)))
    lTx = -10
    Nd = float(num)
    while(lTx<alg):#loop number character alg
      X1 = Tx**alg
      X2 = Tx**(alg+1)
      if(Nd>=X1)and(Nd<X2):
        while(p<=Tx):
          Xa = p*X1
          Xb = (p+1)*X1
          if(Xa<=Nd)and(Xb>Nd):
            Nd-=p*X1
            U.append(tr[p])#decrement block
            if(alg==0):
              U.append('.')#float atribuition
          if((p+1)<Tx):
            p = (p+1)
          else:
            break
      else:
        for caracter in U:
          if caracter != '0':
            U.append('0')
        if(alg==0):
              U += '.'#float atribuition
      p=0;
      alg-=1
      re=""
      i=0
      while(i<len(U)):
              re+=U[i]
              i+=1
    return re
bas = int(input("Input defalt base < 64:\n"))
if(0>bas<62):
bas = 60

while 1:
  n = (input("Input: [number] [base]\n")).split()
  if(len(n)==2):
    res = contri(float(n[0]),int(n[1]));
    print(res)
  else:
    res = contri(float(n[0]),bas);
    print(res)

但是在非数字字符识别部分,它给了一些工作,它取决于蛮力脚本(比较字符),基础比较(自动结果): number in base_nfloat in bases_n