2 #ifdef BN_MP_READ_RADIX_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 /* read a string [ASCII] in a given radix */
19 int mp_read_radix (mp_int
* a
, const char *str
, int radix
)
24 /* zero the digit bignum */
27 /* make sure the radix is ok */
28 if (radix
< 2 || radix
> 64) {
32 /* if the leading digit is a
33 * minus set the sign to negative.
42 /* set the integer to the default of zero */
45 /* process each digit of the string */
47 /* if the radix < 36 the conversion is case insensitive
48 * this allows numbers like 1AB and 1ab to represent the same value
51 ch
= (char) ((radix
< 36) ? toupper (*str
) : *str
);
52 for (y
= 0; y
< 64; y
++) {
53 if (ch
== mp_s_rmap
[y
]) {
58 /* if the char was found in the map
59 * and is less than the given radix add it
60 * to the number, otherwise exit the loop.
63 if ((res
= mp_mul_d (a
, (mp_digit
) radix
, a
)) != MP_OKAY
) {
66 if ((res
= mp_add_d (a
, (mp_digit
) y
, a
)) != MP_OKAY
) {
75 /* set the sign only if a != 0 */
76 if (mp_iszero(a
) != 1) {
83 /* $Source: /cvs/libtom/libtommath/bn_mp_read_radix.c,v $ */
84 /* $Revision: 1.5 $ */
85 /* $Date: 2006/12/28 01:25:13 $ */