2024-03-28 21:22:28 发布
网友
我正在为我的网站做一个网址缩短器,我目前的计划(我愿意接受建议)是使用一个节点ID来生成缩短的网址。因此,理论上,节点26可能是short.com/z,节点1可能是short.com/a,节点52可能是short.com/Z,节点104可能是short.com/ZZ。当用户转到那个URL时,我需要反转这个过程(显然)。
short.com/z
short.com/a
short.com/Z
short.com/ZZ
我可以想出一些笨拙的方法来解决这个问题,但我想还有更好的方法。有什么建议吗?
ASCII到int:
ord('a')
给出97
97
回到字符串:
str(unichr(97))
str(chr(97))
给出'a'
'a'
如果多个字符绑定在一个整数/长字符内,我的问题是:
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))
产生'0123456789'和x = 227581098929683594426425L
'0123456789'
x = 227581098929683594426425L
>>> ord("a") 97 >>> chr(97) 'a'
ASCII到int:
给出
97
回到字符串:
str(unichr(97))
str(chr(97))
给出
'a'
如果多个字符绑定在一个整数/长字符内,我的问题是:
产生
'0123456789'
和x = 227581098929683594426425L
相关问题 更多 >
编程相关推荐