|
1 /* |
|
2 * basecvt.c |
|
3 * |
|
4 * Convert integer values specified on the command line from one input |
|
5 * base to another. Accepts input and output bases between 2 and 36 |
|
6 * inclusive. |
|
7 * |
|
8 * This Source Code Form is subject to the terms of the Mozilla Public |
|
9 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
10 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
11 |
|
12 #include <stdio.h> |
|
13 #include <stdlib.h> |
|
14 #include <string.h> |
|
15 |
|
16 #include "mpi.h" |
|
17 |
|
18 #define IBASE 10 |
|
19 #define OBASE 16 |
|
20 #define USAGE "Usage: %s ibase obase [value]\n" |
|
21 #define MAXBASE 64 |
|
22 #define MINBASE 2 |
|
23 |
|
24 int main(int argc, char *argv[]) |
|
25 { |
|
26 int ix, ibase = IBASE, obase = OBASE; |
|
27 mp_int val; |
|
28 |
|
29 ix = 1; |
|
30 if(ix < argc) { |
|
31 ibase = atoi(argv[ix++]); |
|
32 |
|
33 if(ibase < MINBASE || ibase > MAXBASE) { |
|
34 fprintf(stderr, "%s: input radix must be between %d and %d inclusive\n", |
|
35 argv[0], MINBASE, MAXBASE); |
|
36 return 1; |
|
37 } |
|
38 } |
|
39 if(ix < argc) { |
|
40 obase = atoi(argv[ix++]); |
|
41 |
|
42 if(obase < MINBASE || obase > MAXBASE) { |
|
43 fprintf(stderr, "%s: output radix must be between %d and %d inclusive\n", |
|
44 argv[0], MINBASE, MAXBASE); |
|
45 return 1; |
|
46 } |
|
47 } |
|
48 |
|
49 mp_init(&val); |
|
50 while(ix < argc) { |
|
51 char *out; |
|
52 int outlen; |
|
53 |
|
54 mp_read_radix(&val, argv[ix++], ibase); |
|
55 |
|
56 outlen = mp_radix_size(&val, obase); |
|
57 out = calloc(outlen, sizeof(char)); |
|
58 mp_toradix(&val, out, obase); |
|
59 |
|
60 printf("%s\n", out); |
|
61 free(out); |
|
62 } |
|
63 |
|
64 mp_clear(&val); |
|
65 |
|
66 return 0; |
|
67 } |