1 /* mpz_get_str (string, base, mp_src) -- Convert the multiple precision
2 number MP_SRC to a string STRING of base BASE. If STRING is NULL
3 allocate space for the result. In any case, return a pointer to the
4 result. If STRING is not NULL, the caller must ensure enough space is
5 available to store the result.
7 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Library General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
19 License for more details.
21 You should have received a copy of the GNU Library General Public License
22 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
23 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 MA 02111-1307, USA. */
31 mpz_get_str (char *res_str, int base, mpz_srcptr x)
33 mpz_get_str (res_str, base, x)
40 mp_size_t x_size = x->_mp_size;
53 num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
58 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
61 /* We allways allocate space for the string. If the caller passed a
62 NULL pointer for RES_STR, we allocate permanent space and return
63 a pointer to that to the caller. */
64 str_size = ((size_t) (ABS (x_size) * BITS_PER_MP_LIMB
65 * __mp_bases[base].chars_per_bit_exactly)) + 3;
68 /* We didn't get a string from the user. Allocate one (and return
70 res_str = (char *) (*_mp_allocate_func) (str_size);
71 /* Make str, the variable used for raw result from mpn_get_str,
72 point to the same string, but just after a possible minus sign. */
73 str = (unsigned char *) res_str + 1;
77 /* Use TMP_ALLOC to get temporary space, since we need a few extra bytes
78 that we can't expect to caller to supply us with. */
79 str = (unsigned char *) TMP_ALLOC (str_size);
97 /* Move the number to convert into temporary space, since mpn_get_str
98 clobbers its argument + needs one extra high limb.... */
99 xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB);
100 MPN_COPY (xp, x->_mp_d, x_size);
102 str_size = mpn_get_str (str, base, xp, x_size);
104 /* mpn_get_str might make some leading zeros. Skip them. */
111 /* Translate result to printable chars and move result to RES_STR. */
112 for (i = 0; i < str_size; i++)
113 res_str[i] = num_to_text[str[i]];
114 res_str[str_size] = 0;