Created
June 17, 2010 01:54
-
-
Save LindseyB/441559 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* toBase26(int num){ | |
char* str; | |
str = malloc(sizeof(char)*10); | |
char* temp = str; | |
while(num > 0){ | |
temp[0] = (num%26) + 0x40; | |
temp++; | |
num /= 26; | |
} | |
temp = '\0'; | |
return str; | |
} | |
int toBase10(char* str){ | |
int len = strlen(str); | |
int num = 0; | |
int i; | |
for(i = len-1; i >= 0; i--){ | |
num = num*26 + ((int)str[i] - 0x40); | |
} | |
return num; | |
} | |
int main(){ | |
printf("%s\n", toBase26(27)); // prints AA | |
printf("%d\n", toBase10("AA")); // prints 27 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not really base 26 - but I am converting numbers <-> letters in some form. Based on base 26 to/from base 10 conversion.