|
|
|
|
|
by gojomo
6358 days ago
|
|
There's a shorter way without a bunch of mysterious constants: // decode a 5-digit base52 number
unsigned long b52(const char *c) {
unsigned long factor = 1;
unsigned long result = 0;
for(int i = 0; i < 5; i++) {
result += factor * b52map[c[i]];
factor *= 52;
}
return result;
}
(52^5 < 2^32 so an 'unsigned long' is plenty; 52^11 < 2^64 so you could get an 11-digit base52 number into an 'unsigned long long'. I'd be tempted to generalize the function to handle 0-11 digits, by requiring the parameter to be null-terminated or passing in a length.) |
|