michael@0: /* michael@0: * basecvt.c michael@0: * michael@0: * Convert integer values specified on the command line from one input michael@0: * base to another. Accepts input and output bases between 2 and 36 michael@0: * inclusive. michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "mpi.h" michael@0: michael@0: #define IBASE 10 michael@0: #define OBASE 16 michael@0: #define USAGE "Usage: %s ibase obase [value]\n" michael@0: #define MAXBASE 64 michael@0: #define MINBASE 2 michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ix, ibase = IBASE, obase = OBASE; michael@0: mp_int val; michael@0: michael@0: ix = 1; michael@0: if(ix < argc) { michael@0: ibase = atoi(argv[ix++]); michael@0: michael@0: if(ibase < MINBASE || ibase > MAXBASE) { michael@0: fprintf(stderr, "%s: input radix must be between %d and %d inclusive\n", michael@0: argv[0], MINBASE, MAXBASE); michael@0: return 1; michael@0: } michael@0: } michael@0: if(ix < argc) { michael@0: obase = atoi(argv[ix++]); michael@0: michael@0: if(obase < MINBASE || obase > MAXBASE) { michael@0: fprintf(stderr, "%s: output radix must be between %d and %d inclusive\n", michael@0: argv[0], MINBASE, MAXBASE); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: mp_init(&val); michael@0: while(ix < argc) { michael@0: char *out; michael@0: int outlen; michael@0: michael@0: mp_read_radix(&val, argv[ix++], ibase); michael@0: michael@0: outlen = mp_radix_size(&val, obase); michael@0: out = calloc(outlen, sizeof(char)); michael@0: mp_toradix(&val, out, obase); michael@0: michael@0: printf("%s\n", out); michael@0: free(out); michael@0: } michael@0: michael@0: mp_clear(&val); michael@0: michael@0: return 0; michael@0: }