Skip to content

Instantly share code, notes, and snippets.

@LindseyB
Created June 17, 2010 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LindseyB/441559 to your computer and use it in GitHub Desktop.
Save LindseyB/441559 to your computer and use it in GitHub Desktop.
#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;
}
@LindseyB
Copy link
Author

Not really base 26 - but I am converting numbers <-> letters in some form. Based on base 26 to/from base 10 conversion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment