擅长:python、mysql、java
<p>这并不难:</p>
<pre><code>def str2int(s, chars):
i = 0
for c in reversed(s):
i *= len(chars)
i += chars.index(c)
return i
def int2str(i, chars):
s = ""
while i:
s += chars[i % len(chars)]
i //= len(chars)
return s
</code></pre>
<p>示例:</p>
<pre><code>>>> chars = "".join(str(n) for n in range(10)) + "abcdefg"
>>> str2int("0235abg02", chars)
14354195089
>>> int2str(_, chars)
'0235abg02'
</code></pre>
<p>基本上,如果您想将<em>n</em>字符编码成一个整数,您可以将其解释为<em>base-n</em>。</p>