码迷,mamicode.com
首页 > 编程语言 > 详细

Python版任意进制转换

时间:2016-04-26 23:56:35      阅读:1070      评论:0      收藏:0      [点我收藏+]

标签:

def decimalToAny(num,n):
   baseStr = {10:"a",11:"b",12:"c",13:"d",14:"e",15:"f",16:"g",17:"h",18:"i",19:"j"}
   new_num_str = ""
   while num != 0:
       remainder = num % n
       if 20 > remainder > 9:
           remainder_string = baseStr[remainder]
       elif remainder >=20:
           remainder_string = "("+str(remainder)+")"
       else:
           remainder_string = str(remainder)
       new_num_str = remainder_string+new_num_str
       num = num / n
   return new_num_str

#将任意进制数转换成十进制
def anyToDecimal(num,n):
   baseStr = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,
              "a":10,"b":11,"c":12,"d":13,"e":14,"f":15,"g":16,"h":17,"i":18,"j":19}
   new_num = 0
   nNum = len(num) - 1
   for i in num:         
       new_num = new_num  + baseStr[i]*pow(n,nNum)
       nNum = nNum -1 
   return new_num

 

Python版任意进制转换

标签:

原文地址:http://www.cnblogs.com/test404/p/5436911.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!