Python允许从给定基数的字符串中轻松创建一个整数
int(str, base).
我想执行相反的操作:从一个整数创建一个字符串,
例如,我想要一些函数int2base(num, base),这样:
int(int2base(x, b), b) == x
函数名/参数的顺序并不重要。
对于int()将接受的任何以b为底的数字x。
这是一个很容易写的函数:事实上,它比在这个问题中描述它更容易。然而,我觉得我一定是错过了什么。
我知道函数bin, oct, hex,但我不能使用它们的几个原因:
这些函数在旧版本的Python中不可用,我需要与(2.2)兼容
我想要一个通解对于不同的碱都可以用同样的方式表示
我想允许2 8 16以外的底数
相关的
Python优雅的int(string, base)逆函数
在python中使用递归的整数到base-x系统
Python中的Base 62转换
如何在Python中将整数转换为最短的url安全字符串?
我为此做了一个小包裹。
我建议你使用我的bases.py https://github.com/kamijoutouma/bases.py,它的灵感来自于bases.js
from bases import Bases
bases = Bases()
bases.toBase16(200) // => 'c8'
bases.toBase(200, 16) // => 'c8'
bases.toBase62(99999) // => 'q0T'
bases.toBase(200, 62) // => 'q0T'
bases.toAlphabet(300, 'aAbBcC') // => 'Abba'
bases.fromBase16('c8') // => 200
bases.fromBase('c8', 16) // => 200
bases.fromBase62('q0T') // => 99999
bases.fromBase('q0T', 62) // => 99999
bases.fromAlphabet('Abba', 'aAbBcC') // => 300
参考https://github.com/kamijoutouma/bases.py#known-basesalphabets
哪些基是可用的
编辑:
PIP link https://pypi.python.org/pypi/bases.py/0.2.2
我写了这个函数,我用它来编码不同的碱基。我还提供了通过值“offset”来移动结果的方法。如果你想编码到64进制以上,但保持可显示字符(如95进制),这是有用的。
我还试图避免反转输出“列表”,并尽量减少计算操作。pow(base)数组是根据需要计算的,并保留用于对函数的其他调用。
输出是一个二进制字符串
pows = {}
######################################################
def encode_base(value,
base = 10,
offset = 0) :
"""
Encode value into a binary string, according to the desired base.
Input :
value : Any positive integer value
offset : Shift the encoding (eg : Starting at chr(32))
base : The base in which we'd like to encode the value
Return : Binary string
Example : with : offset = 32, base = 64
100 -> !D
200 -> #(
"""
# Determine the number of loops
try :
pb = pows[base]
except KeyError :
pb = pows[base] = {n : base ** n for n in range(0, 8) if n < 2 ** 48 -1}
for n in pb :
if value < pb[n] :
n -= 1
break
out = []
while n + 1 :
b = pb[n]
out.append(chr(offset + value // b))
n -= 1
value %= b
return ''.join(out).encode()