Back to index

lightning-sunbird  0.9+nobinonly
basecvt.c
Go to the documentation of this file.
00001 /*
00002  *  basecvt.c
00003  *
00004  *  Convert integer values specified on the command line from one input
00005  *  base to another.  Accepts input and output bases between 2 and 36
00006  *  inclusive.
00007  *
00008  * ***** BEGIN LICENSE BLOCK *****
00009  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00010  *
00011  * The contents of this file are subject to the Mozilla Public License Version
00012  * 1.1 (the "License"); you may not use this file except in compliance with
00013  * the License. You may obtain a copy of the License at
00014  * http://www.mozilla.org/MPL/
00015  *
00016  * Software distributed under the License is distributed on an "AS IS" basis,
00017  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00018  * for the specific language governing rights and limitations under the
00019  * License.
00020  *
00021  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00022  *
00023  * The Initial Developer of the Original Code is
00024  * Michael J. Fromberger.
00025  * Portions created by the Initial Developer are Copyright (C) 1998
00026  * the Initial Developer. All Rights Reserved.
00027  *
00028  * Contributor(s):
00029  *
00030  * Alternatively, the contents of this file may be used under the terms of
00031  * either the GNU General Public License Version 2 or later (the "GPL"), or
00032  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00033  * in which case the provisions of the GPL or the LGPL are applicable instead
00034  * of those above. If you wish to allow use of your version of this file only
00035  * under the terms of either the GPL or the LGPL, and not to allow others to
00036  * use your version of this file under the terms of the MPL, indicate your
00037  * decision by deleting the provisions above and replace them with the notice
00038  * and other provisions required by the GPL or the LGPL. If you do not delete
00039  * the provisions above, a recipient may use your version of this file under
00040  * the terms of any one of the MPL, the GPL or the LGPL.
00041  *
00042  * ***** END LICENSE BLOCK ***** */
00043 /* $Id: basecvt.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00044 
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 
00049 #include "mpi.h"
00050 
00051 #define IBASE     10
00052 #define OBASE     16
00053 #define USAGE     "Usage: %s ibase obase [value]\n"
00054 #define MAXBASE   64
00055 #define MINBASE   2
00056 
00057 int main(int argc, char *argv[])
00058 {
00059   int    ix, ibase = IBASE, obase = OBASE;
00060   mp_int val;
00061 
00062   ix = 1;
00063   if(ix < argc) {
00064     ibase = atoi(argv[ix++]);
00065     
00066     if(ibase < MINBASE || ibase > MAXBASE) {
00067       fprintf(stderr, "%s: input radix must be between %d and %d inclusive\n",
00068              argv[0], MINBASE, MAXBASE);
00069       return 1;
00070     }
00071   }
00072   if(ix < argc) {
00073     obase = atoi(argv[ix++]);
00074 
00075     if(obase < MINBASE || obase > MAXBASE) {
00076       fprintf(stderr, "%s: output radix must be between %d and %d inclusive\n",
00077              argv[0], MINBASE, MAXBASE);
00078       return 1;
00079     }
00080   }
00081 
00082   mp_init(&val);
00083   while(ix < argc) {
00084     char  *out;
00085     int    outlen;
00086 
00087     mp_read_radix(&val, argv[ix++], ibase);
00088 
00089     outlen = mp_radix_size(&val, obase);
00090     out = calloc(outlen, sizeof(char));
00091     mp_toradix(&val, out, obase);
00092 
00093     printf("%s\n", out);
00094     free(out);
00095   }
00096 
00097   mp_clear(&val);
00098 
00099   return 0;
00100 }