int test_qtostr(testspec_t *t, FILE *ofp) { mp_rat in[2]; long radix; mp_result res, len; if(!parse_rat_values(t, in, NULL, NULL)) return imath_errno = MP_BADARG, 0; trim_line(t->input[1]); if(!read_long(&radix, t->input[1])) return imath_errno = MP_BADARG, 0; if(radix < MP_MIN_RADIX || radix > MP_MAX_RADIX) { fprintf(stderr, "Line %d: Radix %ld out of range\n", t->line, radix); return imath_errno = MP_RANGE, 0; } trim_line(t->output[0]); len = mp_rat_string_len(in[0], radix); if((res = mp_rat_to_string(in[0], radix, g_output, len)) != MP_OK) return imath_errno = res, 0; if(strcmp(t->output[0], g_output) != 0) return imath_errno = OTHER_ERROR, 0; return 1; }
int main(int argc, char *argv[]) { mp_size radix = 10; /* Default output radix */ mpq_t value; mp_result res; char *endp; if(argc < 2) { fprintf(stderr, "Usage: input <value> [output-base]\n"); return 1; } if(argc > 2) { if((radix = atoi(argv[2])) < MP_MIN_RADIX || (radix > MP_MAX_RADIX)) { fprintf(stderr, "Error: Specified radix is out of range (%d)\n", radix); return 1; } } /* Initialize a new value, initially zero; illustrates how to check for errors (e.g., out of memory) and display a message. */ if((res = mp_rat_init(&value)) != MP_OK) { fprintf(stderr, "Error in mp_rat_init(): %s\n", mp_error_string(res)); return 1; } /* Read value in base 10 */ if((res = mp_rat_read_ustring(&value, 0, argv[1], &endp)) != MP_OK) { fprintf(stderr, "Error in mp_rat_read_ustring(): %s\n", mp_error_string(res)); if(res == MP_TRUNC) fprintf(stderr, " -- remaining input is: %s\n", endp); mp_rat_clear(&value); return 1; } printf("Here is your value in base %d\n", radix); { mp_result buf_size, res; char *obuf; if(mp_rat_is_integer(&value)) { /* Allocate a buffer big enough to hold the given value, including sign and zero terminator. */ buf_size = mp_int_string_len(MP_NUMER_P(&value), radix); obuf = malloc(buf_size); /* Convert the value to a string in the desired radix. */ if((res = mp_int_to_string(MP_NUMER_P(&value), radix, obuf, buf_size)) != MP_OK) { fprintf(stderr, "Converstion to base %d failed: %s\n", radix, mp_error_string(res)); mp_rat_clear(&value); return 1; } } else { /* Allocate a buffer big enough to hold the given value, including sign and zero terminator. */ buf_size = mp_rat_string_len(&value, radix); obuf = malloc(buf_size); /* Convert the value to a string in the desired radix. */ if((res = mp_rat_to_string(&value, radix, obuf, buf_size)) != MP_OK) { fprintf(stderr, "Conversion to base %d failed: %s\n", radix, mp_error_string(res)); mp_rat_clear(&value); return 1; } } fputs(obuf, stdout); fputc('\n', stdout); free(obuf); } /* When you are done with a value, it must be "cleared" to release the memory it occupies */ mp_rat_clear(&value); return 0; }