ARM Shanghai IoT Team (Internal) / newMiniTLS-GPL

Fork of MiniTLS-GPL by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fp_toradix.c Source File

fp_toradix.c

00001 /* TomsFastMath, a fast ISO C bignum library.
00002  * 
00003  * This project is meant to fill in where LibTomMath
00004  * falls short.  That is speed ;-)
00005  *
00006  * This project is public domain and free for all purposes.
00007  * 
00008  * Tom St Denis, tomstdenis@gmail.com
00009  */
00010 #include <tfm.h>
00011 
00012 int fp_toradix(fp_int *a, char *str, int radix)
00013 {
00014   int     digs;
00015   fp_int  t;
00016   fp_digit d;
00017   char   *_s = str;
00018 
00019   /* check range of the radix */
00020   if (radix < 2 || radix > 64) {
00021     return FP_VAL;
00022   }
00023 
00024   /* quick out if its zero */
00025   if (fp_iszero(a) == 1) {
00026      *str++ = '0';
00027      *str = '\0';
00028      return FP_OKAY;
00029   }
00030 
00031   fp_init_copy(&t, a);
00032 
00033   /* if it is negative output a - */
00034   if (t.sign == FP_NEG) {
00035     ++_s;
00036     *str++ = '-';
00037     t.sign = FP_ZPOS;
00038   }
00039 
00040   digs = 0;
00041   while (fp_iszero (&t) == FP_NO) {
00042     fp_div_d (&t, (fp_digit) radix, &t, &d);
00043     *str++ = fp_s_rmap[d];
00044     ++digs;
00045   }
00046 
00047   /* reverse the digits of the string.  In this case _s points
00048    * to the first digit [exluding the sign] of the number]
00049    */
00050   fp_reverse ((unsigned char *)_s, digs);
00051 
00052   /* append a NULL so the string is properly terminated */
00053   *str = '\0';
00054   return FP_OKAY;
00055 }
00056 
00057 /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_toradix.c,v $ */
00058 /* $Revision: 1.2 $ */
00059 /* $Date: 2007/02/27 02:38:44 $ */