将 int 转换为 ASCII 并返回 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3673428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:10:25  来源:igfitidea点击:

Convert int to ASCII and back in Python

pythonintegerasciiencode

提问by mlissner

I'm working on making a URL shortener for my site, and my current plan (I'm open to suggestions) is to use a node ID to generate the shortened URL. So, in theory, node 26 might be short.com/z, node 1 might be short.com/a, node 52 might be short.com/Z, and node 104 might be short.com/ZZ. When a user goes to that URL, I need to reverse the process (obviously).

我正在为我的网站制作一个 URL 缩短器,我目前的计划(我愿意接受建议)是使用节点 ID 来生成缩短的 URL。因此,理论上,节点 26 可能是short.com/z,节点 1 可能是short.com/a,节点 52 可能是short.com/Z,节点 104 可能是short.com/ZZ。当用户访问该 URL 时,我需要反转该过程(显然)。

I can think of some kludgy ways to go about this, but I'm guessing there are better ones. Any suggestions?

我可以想到一些笨拙的方法来解决这个问题,但我猜有更好的方法。有什么建议?

回答by Omnifarious

Use hex(id)[2:]and int(urlpart, 16). There are other options. base32 encoding your id could work as well, but I don't know that there's any library that does base32 encoding built into Python.

使用hex(id)[2:]int(urlpart, 16)。还有其他选择。base32 编码您的 id 也可以工作,但我不知道有任何库可以在 Python 中进行 base32 编码。

Apparently a base32 encoder was introduced in Python 2.4 with the base64 module. You might try using b32encodeand b32decode. You should give Truefor both the casefoldand map01options to b32decodein case people write down your shortened URLs.

显然,带有base64 模块的Python 2.4 中引入了 base32 编码器。您可以尝试使用b32encodeb32decode。你应该给True两者的casefoldmap01期权b32decode的情况下,人们写下你的短网址。

Actually, I take that back. I still think base32 encoding is a good idea, but that module is not useful for the case of URL shortening. You could look at the implementation in the module and make your own for this specific case. :-)

其实,我收回。我仍然认为 base32 编码是一个好主意,但该模块对于 URL 缩短的情况没有用。您可以查看模块中的实现,并针对这种特定情况创建自己的实现。:-)

回答by Dominic Bou-Samra

ASCII to int:

ASCII 到整数:

ord('a')

gives 97

97

And back to a string:

回到一个字符串:

  • in Python2: str(unichr(97))
  • in Python3: chr(97)
  • 在 Python2 中: str(unichr(97))
  • 在 Python3 中: chr(97)

gives 'a'

'a'

回答by Ivo Wetzel

What about BASE58 encoding the URL? Like for example flickr does.

BASE58 编码 URL 怎么样?就像 flickr 一样。

# note the missing lowercase L and the zero etc.
BASE58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ' 
url = ''
while node_id >= 58:
    div, mod = divmod(node_id, 58)
    url = BASE58[mod] + url
    node_id = int(div)

return 'http://short.com/%s' % BASE58[node_id] + url

Turning that back into a number isn't a big deal either.

把它变成一个数字也没什么大不了的。

回答by renatov

>>> ord("a")
97
>>> chr(97)
'a'

回答by Matthew Davis

If multiple characters are bound inside a single integer/long, as was my issue:

如果多个字符绑定在单个整数/长整数内,就像我的问题:

s = '0123456789'
nchars = len(s)
# string to int or long. Type depends on nchars
x = sum(ord(s[byte])<<8*(nchars-byte-1) for byte in range(nchars))
# int or long to string
''.join(chr((x>>8*(nchars-byte-1))&0xFF) for byte in range(nchars))

Yields '0123456789'and x = 227581098929683594426425L

产量'0123456789'x = 227581098929683594426425L