Last active
September 29, 2015 17:25
-
-
Save akoluthic/c19ef38c2b0bfc21e571 to your computer and use it in GitHub Desktop.
base 26 uppercase alpha string to integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
int ipow(int base, int exp) | |
{ | |
int r = 1; | |
while (exp) { | |
if (exp & 1) | |
r *= base; | |
exp >>= 1; | |
base *= base; | |
} | |
return r; | |
} | |
int seed2int(const char *c) | |
{ | |
int t = 0; | |
size_t ln = strlen(c); | |
for (int i = 0; i < ln; ++i) | |
t += ipow(26, ln - 1 - i) * (c[i] - '@'); | |
return t; | |
} | |
int main(void) | |
{ | |
const char *c = "A"; | |
printf("%d\n", seed2int(c)); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment