python 自己写的十进制转所有进制

#-*- coding:utf-8 -*-
#-------------------------------------------------------------------------------
# Name: ??1
# Purpose:
#
# Author: qyl
#
# Created: 23-11-2011
# Copyright: (c) Administrator 2011
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python


def bin2(num,jz):
if type(num) != type(1) or type(jz) != type(1):
return "para error!"
if jz > 20:
return "para jz should little than 20"
t = num
r = ''
#如果小于进制数,直接转换
if t < jz:
stmp = `t`
if t > 9:
stmp = chr(55 + t)
return stmp
#如果不小于2倍的进制数
while t >= jz*2:
tmp = t%jz
stmp = str(tmp)
if tmp > 9:
stmp = chr(55 + tmp)
r = stmp + r
t = t/jz
#如果除完结果小于进制数,进行如下转换
if t < jz:
stmp = `t`
if t > 9:
stmp = chr(55 + t)
r = stmp + r
return r
#如果除完结果大于进制数,进行如下转换
for i in range(jz,jz*2):
if t == i:
temp = `i - jz`
if i - jz > 9:
temp = chr(55 + i - jz)
r = "1" + temp + r
break
return r

def test():
for i in range(100000):
for j in range(2,17):
c = bin2(i,j)
t = int(c,j)
if t != i:
print c,
print i,
print 'no'
break
print "no error"

def main():
test()


if __name__ == '__main__':
main()

你可能感兴趣的:(python 自己写的十进制转所有进制)