Skip to content

Instantly share code, notes, and snippets.

@lemoce
Created April 18, 2018 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemoce/1cd9b23e78add748282ef6c76ac2c761 to your computer and use it in GitHub Desktop.
Save lemoce/1cd9b23e78add748282ef6c76ac2c761 to your computer and use it in GitHub Desktop.
#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