进制之间互相转换代码
来源:互联网 发布:淘宝网店数码名 编辑:程序博客网 时间:2024/05/21 06:14
#--coding:utf8--
#file:baseconvert.py
BASE2 = "01"
BASE10 = "0123456789"
BASE16 = "0123456789ABCDEF"
BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
BASE36 = "0123456789abcdefghijklmnopqrstuvwxyz"
def baseconvert(number,fromdigits,todigits):
""" converts a "number" between two bases of arbitrary digits
The input number is assumed to be a string of digits from the
fromdigits string (which is in order of smallest to largest
digit). The return value is a string of elements from todigits
(ordered in the same way). The input and output bases are
determined from the lengths of the digit strings. Negative
signs are passed through.
decimal to binary
>>> baseconvert(555,BASE10,BASE2)
'1000101011'
binary to decimal
>>> baseconvert('1000101011',BASE2,BASE10)
'555'
integer interpreted as binary and converted to decimal (!)
>>> baseconvert(1000101011,BASE2,BASE10)
'555'
base10 to base4
>>> baseconvert(99,BASE10,"0123")
'1203'
base4 to base5 (with alphabetic digits)
>>> baseconvert(1203,"0123","abcde")
'dee'
base5, alpha digits back to base 10
>>> baseconvert('dee',"abcde",BASE10)
'99'
decimal to a base that uses A-Z0-9a-z for its digits
>>> baseconvert(257938572394L,BASE10,BASE62)
'E78Lxik'
..convert back
>>> baseconvert('E78Lxik',BASE62,BASE10)
'257938572394'
binary to a base with words for digits (the function cannot convert this back)
>>> baseconvert('1101',BASE2,('Zero','One'))
'OneOneZeroOne'
"""
if str(number)[0]=='-':
number = str(number)[1:]
neg=1
else:
neg=0
# make an integer out of the number
x=long(0)
for digit in str(number):
x = x*len(fromdigits) + fromdigits.index(digit)
# create the result in base 'len(todigits)'
res=""
while x>0:
digit = x % len(todigits)
res = todigits[digit] + res
x /= len(todigits)
if neg:
res = "-"+res
return res
#file:baseconvert.py
BASE2 = "01"
BASE10 = "0123456789"
BASE16 = "0123456789ABCDEF"
BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
BASE36 = "0123456789abcdefghijklmnopqrstuvwxyz"
def baseconvert(number,fromdigits,todigits):
""" converts a "number" between two bases of arbitrary digits
The input number is assumed to be a string of digits from the
fromdigits string (which is in order of smallest to largest
digit). The return value is a string of elements from todigits
(ordered in the same way). The input and output bases are
determined from the lengths of the digit strings. Negative
signs are passed through.
decimal to binary
>>> baseconvert(555,BASE10,BASE2)
'1000101011'
binary to decimal
>>> baseconvert('1000101011',BASE2,BASE10)
'555'
integer interpreted as binary and converted to decimal (!)
>>> baseconvert(1000101011,BASE2,BASE10)
'555'
base10 to base4
>>> baseconvert(99,BASE10,"0123")
'1203'
base4 to base5 (with alphabetic digits)
>>> baseconvert(1203,"0123","abcde")
'dee'
base5, alpha digits back to base 10
>>> baseconvert('dee',"abcde",BASE10)
'99'
decimal to a base that uses A-Z0-9a-z for its digits
>>> baseconvert(257938572394L,BASE10,BASE62)
'E78Lxik'
..convert back
>>> baseconvert('E78Lxik',BASE62,BASE10)
'257938572394'
binary to a base with words for digits (the function cannot convert this back)
>>> baseconvert('1101',BASE2,('Zero','One'))
'OneOneZeroOne'
"""
if str(number)[0]=='-':
number = str(number)[1:]
neg=1
else:
neg=0
# make an integer out of the number
x=long(0)
for digit in str(number):
x = x*len(fromdigits) + fromdigits.index(digit)
# create the result in base 'len(todigits)'
res=""
while x>0:
digit = x % len(todigits)
res = todigits[digit] + res
x /= len(todigits)
if neg:
res = "-"+res
return res
- 进制之间互相转换代码
- Java进制之间互相转换
- 2进制数据与图片之间的互相转换
- 整型和二进制之间的互相转换(代码)
- 进制互相转换
- Java的十六进制与字符串之间互相转换函数(16进制与String转换)
- QML和C++代码之间互相调用及参数之间的转换
- MS SQL Server10进制和16进制之间的互相转换
- 【黑马程序员】Java基础01:进制之间的互相转换
- 各种进制的字符串与数值之间的互相转换
- 五分钟教会你任何进制之间的互相转换
- 字符串与数字之间的互相转换
- List和string之间的互相转换
- 字符串与数字之间的互相转换
- string和numeric之间的互相转换
- List和string之间的互相转换
- word和pdf之间的互相转换
- 图片和字节之间的互相转换
- 手机号码为什么是11位??
- Wordpress在Win-iis中的完美rewrite实现方法
- Wordpress在Win-iis中的完美rewrite实现方法
- 性能测试利器之---WAS
- Hibernate 学习笔记(1)
- 进制之间互相转换代码
- Date and Calendar使用方法详解(1)
- 将 Oracle XML Publisher 与 Oracle JDeveloper 10g 集成
- 将要到来的2009
- div左图右文布局效果
- windows2008开始菜单反映缓慢的问题
- The Linux Kernel HOWTO
- 端口大全及端口关闭方法
- JAVA学习提高之---- String.split(String,int)使用