# 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
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))
/**
* 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;
}