48 lines
808 B
C
48 lines
808 B
C
#include <limits.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "baseconv.h"
|
|
|
|
int main(void) {
|
|
uintmax_t num;
|
|
printf("Number? ");
|
|
if (scanf("%ju", &num) != 1) {
|
|
fprintf(stderr, "Invalid input\n");
|
|
return 1;
|
|
}
|
|
|
|
size_t base;
|
|
printf("Base? ");
|
|
if (scanf("%zu", &base) != 1) {
|
|
fprintf(stderr, "Invalid input\n");
|
|
return 1;
|
|
}
|
|
|
|
size_t bufsize;
|
|
printf("Buffer size? ");
|
|
if (scanf("%zu", &bufsize) != 1) {
|
|
fprintf(stderr, "Invalid input\n");
|
|
return 1;
|
|
}
|
|
|
|
char *buf = malloc(bufsize);
|
|
if (!buf) {
|
|
fprintf(stderr, "Failed to allocate %zu bytes\n", bufsize);
|
|
return 1;
|
|
}
|
|
|
|
if (!baseconv(buf, bufsize, base, num)) {
|
|
fprintf(stderr, "Conversion error\n");
|
|
free(buf);
|
|
return 1;
|
|
}
|
|
|
|
printf("%s\n", buf);
|
|
|
|
free(buf);
|
|
}
|