我正在为我的网站制作一个URL缩短器,我的当前计划(我愿意听取建议)是使用节点ID生成缩短的URL。因此,理论上,节点26可能是short.com/z
,节点1可能是short.com/a
,节点52可能是short.com/Z
,节点104可能是short.com/ZZ
。当用户访问该URL时,我需要反向该过程(显然)。
我可以想到一些笨拙的方法来解决这个问题,但我猜测还有更好的方法。有什么建议吗?
将ASCII转换为整数:
ord('a')
输出 97
回到字符串:
str(unichr(97))
chr(97)
输出 'a'
>>> ord("a")
97
>>> chr(97)
'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
# 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
使用 hex(id)[2:]
和 int(urlpart, 16)
。还有其他选项。将您的id进行base32编码也可以,但我不知道Python中是否内置了任何执行base32编码的库。
显然,在Python 2.4中引入了一个base32编码器,名为base64模块。您可以尝试使用b32encode
和b32decode
。在人们写下您的缩短URL时,应该为b32decode
的casefold
和map01
选项都设置为True
。
实际上,我撤回之前的说法。我仍然认为base32编码是个好主意,但该模块对于URL缩短的情况并不适用。您可以查看模块中的实现,并为此特定情况制作自己的编码器。 :-)
看来我来晚了,只是想分享一个我经常使用的片段。
/**
* 62 = 26 + 26 +10
*
* @param id
* @return
*/
public String base62(long id) {
StringBuilder sb = new StringBuilder();
while (id >= 62) {
int remainer = (int) (id % 62);
id = id / 62;
sb.append(index2char(remainer));
}
sb.append(index2char(id));
return sb.reverse().toString();
}
public long reverseBase62(String s) {
long r = 0;
for (int i = 0; i < s.length(); i++) {
r = r * 62;
int index = char2index(s.charAt(i));
if (index == -1) {
throw new IllegalArgumentException(
String.format("[%s] is in malformation, should only contain 0~9, a~z, A~Z", s));
}
r += index;
}
return r;
}
private char index2char(long index) {
if (index < 10) {
return (char) ('0' + index);
}
if (index < 36) {
return (char) ('a' + index - 10);
}
return (char) ('A' + index - 36);
}
private int char2index(char c) {
if ('0' <= c && c <= '9') {
return c - '0';
}
if ('a' <= c && c <= 'z') {
return c - 'a' + 10;
}
if ('A' <= c && c <= 'Z') {
return c - 'A' + 36;
}
return -1;
}