1 /* TomsFastMath, a fast ISO C bignum library.
2 *
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
5 *
6 * This project is public domain and free for all purposes.
7 *
8 * Tom St Denis, tomstdenis@gmail.com
9 */
10 #include "bignum_fast.h"
11
fp_toradix(fp_int * a,char * str,int radix)12 int fp_toradix(fp_int *a, char *str, int radix)
13 {
14 int digs;
15 fp_int t;
16 fp_digit d;
17 char *_s = str;
18
19 /* check range of the radix */
20 if (radix < 2 || radix > 64) {
21 return FP_VAL;
22 }
23
24 /* quick out if its zero */
25 if (fp_iszero(a) == 1) {
26 *str++ = '0';
27 *str = '\0';
28 return FP_OKAY;
29 }
30
31 fp_init_copy(&t, a);
32
33 /* if it is negative output a - */
34 if (t.sign == FP_NEG) {
35 ++_s;
36 *str++ = '-';
37 t.sign = FP_ZPOS;
38 }
39
40 digs = 0;
41 while (fp_iszero (&t) == FP_NO) {
42 fp_div_d (&t, (fp_digit) radix, &t, &d);
43 *str++ = fp_s_rmap[d];
44 ++digs;
45 }
46
47 /* reverse the digits of the string. In this case _s points
48 * to the first digit [exluding the sign] of the number]
49 */
50 fp_reverse ((unsigned char *)_s, digs);
51
52 /* append a NULL so the string is properly terminated */
53 *str = '\0';
54 return FP_OKAY;
55 }
56
57 /* $Source: /cvs/libtom/tomsfastmath/src/bin/fp_toradix.c,v $ */
58 /* $Revision: 1.2 $ */
59 /* $Date: 2007/02/27 02:38:44 $ */
60