Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- const char digitmap[] = "0123456789abcdef";
- char *num_to_s(int val, int base) {
- char *ret = (char *)malloc(sizeof(char) * 255);
- ret = ret + 255;
- *--ret = '\0';
- do {
- *--ret = digitmap[(int )(val % base)];
- } while (val /= base);
- return ret;
- }
- int main(int argc, char **argv) {
- int n = 1424;
- printf("base 2: %s\n", num_to_s(n, 2));
- printf("base 4: %s\n", num_to_s(n, 4));
- printf("base 8: %s\n", num_to_s(n, 8));
- printf("base 16: %s\n", num_to_s(n, 16));
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement