Created
April 18, 2018 18:14
-
-
Save lemoce/1cd9b23e78add748282ef6c76ac2c761 to your computer and use it in GitHub Desktop.
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> | |
void aux_func(int n, char * buf, int radix, int i, int *len) { | |
int q = n / radix; | |
int r = n % radix; | |
if (q < radix && q >=0 ) { | |
*len = i + 2; | |
*(buf+1) = r + '0'; | |
*(buf) = q + '0'; | |
*(buf+(*len)) = '\0'; | |
return; | |
} else { | |
aux_func(q, buf, radix, i + 1, len); | |
*(buf + (*len - i) - 1) = r + '0'; | |
} | |
} | |
void itoa(int n, char * buf, int radix) { | |
int len = 0; | |
aux_func (n, buf, radix, 0, &len); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
int toBinary; | |
scanf("%x", &toBinary); | |
char binBuf[1024]; | |
char decBuf[1024]; | |
itoa(toBinary, binBuf, 2); | |
printf("%s.\n", binBuf); | |
itoa(toBinary, decBuf, 10); | |
printf("%s.\n", decBuf); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment