1 /* mpz_set_str(mp_dest, string, base) -- Convert the \0-terminated
2 string STRING in base BASE to multiple precision integer in
3 MP_DEST. Allow white space in the string. If BASE == 0 determine
4 the base in the C standard way, i.e. 0xhh...h means base 16,
5 0oo...o means base 8, otherwise assume base 10.
7 Copyright (C) 1991, 1993, 1994, 1996, 1997, 1998, 2000 Free Software
10 This file is part of the GNU MP Library.
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published by
14 the Free Software Foundation; either version 2.1 of the License, or (at your
15 option) any later version.
17 The GNU MP Library is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
20 License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
24 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 MA 02111-1307, USA. */
35 digit_value_in_base (int c, int base)
37 digit_value_in_base (c, base)
60 mpz_set_str (mpz_ptr x, const char *str, int base)
62 mpz_set_str (x, str, base)
76 /* Skip whitespace. */
88 if (digit_value_in_base (c, base == 0 ? 10 : base) < 0)
89 return -1; /* error if no digits */
91 /* If BASE is 0, try to find out the base by looking at the initial
100 if (c == 'x' || c == 'X')
105 else if (c == 'b' || c == 'B')
113 /* Skip leading zeros. */
116 /* Make sure the string does not become empty, mpn_set_str would fail. */
124 str_size = strlen (str - 1);
125 s = begs = (char *) TMP_ALLOC (str_size + 1);
127 /* Remove spaces from the string and convert the result from ASCII to a
129 for (i = 0; i < str_size; i++)
133 int dig = digit_value_in_base (c, base);
146 xsize = (((mp_size_t) (str_size / __mp_bases[base].chars_per_bit_exactly))
147 / BITS_PER_MP_LIMB + 2);
148 if (x->_mp_alloc < xsize)
149 _mpz_realloc (x, xsize);
151 /* Convert the byte array in base BASE to our bignum format. */
152 xsize = mpn_set_str (x->_mp_d, (unsigned char *) begs, str_size, base);
153 x->_mp_size = negative ? -xsize : xsize;