Skip to content

Instantly share code, notes, and snippets.

@akoluthic
Last active September 29, 2015 17:25
Show Gist options
  • Save akoluthic/c19ef38c2b0bfc21e571 to your computer and use it in GitHub Desktop.
Save akoluthic/c19ef38c2b0bfc21e571 to your computer and use it in GitHub Desktop.
base 26 uppercase alpha string to integer
#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