Advertisement
cd62131

Radix Conversion

Feb 5th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. const char digitmap[] = "0123456789abcdef";
  5.  
  6. char *num_to_s(int val, int base) {
  7.   char *ret = (char *)malloc(BUFSIZ);
  8.   ret = ret + BUFSIZ;
  9.   *--ret = '\0';
  10.   do {
  11.     *--ret = digitmap[(int )(val % base)];
  12.   } while (val /= base);
  13.   return ret;
  14. }
  15.  
  16. int main(void) {
  17.   int val, base;
  18.   printf("number base ? ");
  19.   scanf("%d%d", &val, &base);
  20.   printf("%d base %d = %s\n", val, base, num_to_s(val, base));
  21.   return EXIT_SUCCESS;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement