Last active
January 16, 2022 08:49
-
-
Save opsJson/20f13279f3e56cd49f5e4ed9fd8a285e to your computer and use it in GitHub Desktop.
Convert integer to Google Sheets A1 Notation, and vice-versa.
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 <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <math.h> | |
| char itoc(int i) { | |
| if (i < 0) return 0; | |
| return ((i-1) % 26) + 65; | |
| } | |
| int ctoi(char c) { | |
| if (c < 65) return 0; | |
| if (c > 90) return 0; | |
| return c - 64; | |
| } | |
| char* itoa1(int i) { | |
| char *a1; | |
| int count = 0; | |
| int back = i; | |
| while (i) { | |
| count++; | |
| i = (i-1) / 26; | |
| } | |
| a1 = malloc(sizeof(char) * count + 1); | |
| a1[count] = 0; | |
| while (back) { | |
| a1[--count] = itoc(back); | |
| back = (back-1) / 26; | |
| } | |
| return a1; | |
| } | |
| int a1toi(char *a1) { | |
| int i; | |
| int base; | |
| int integer = 0; | |
| size_t length; | |
| length = strlen(a1); | |
| for (i=length-1,base=0; i>=0; i--, base++) { | |
| integer += ctoi(a1[i]) * (int)pow(26, base); | |
| } | |
| return integer; | |
| } | |
| /*/////////////////////////////////// | |
| Testing: | |
| ///////////////////////////////////*/ | |
| int main() { | |
| char *a1; | |
| int i; | |
| a1 = itoa1(704); | |
| i = a1toi(a1); | |
| printf("%s\n", a1); | |
| printf("%i\n", i); | |
| free(a1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment