Last active
December 14, 2020 19:47
-
-
Save fernandolopez/2ffd9fcc8e8bcde79216 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 *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
char *invertir(char *str){ | |
int last = strlen(str) - 1; | |
char swp; | |
for (int i = 0, j = last; i < j; i++, j--){ | |
swp = str[i]; | |
str[i] = str[j]; | |
str[j] = swp; | |
} | |
return str; | |
} | |
char *base(int nro, int base, char *buff){ | |
int mod; | |
int i = 0; | |
if (nro == 0){ | |
strcpy(buff, "0"); | |
return buff; | |
} | |
while (nro > 0) { | |
mod = nro % base; | |
nro = nro / base; | |
buff[i] = digits[mod]; | |
i++; | |
} | |
buff[i] = 0; | |
return invertir(buff); | |
} | |
int main(){ | |
char str[256]; | |
printf("%s\n", base(50, 2, str)); | |
printf("%s\n", base(50, 3, str)); | |
printf("%s\n", base(50, 10, str)); | |
printf("%s\n", base(50, 16, str)); | |
printf("%s\n", base(50, 36, str)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment