1 /* mpz_out_str(stream, base, integer) -- Output to STREAM the multi prec.
2 integer INTEGER in base BASE.
4 Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
29 mpz_out_str (FILE *stream, int base, mpz_srcptr x)
31 mpz_out_str (stream, base, x)
38 mp_size_t x_size = x->_mp_size;
53 num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
58 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
64 return ferror (stream) ? 0 : 1;
77 str_size = ((size_t) (x_size * BITS_PER_MP_LIMB
78 * __mp_bases[base].chars_per_bit_exactly)) + 3;
79 str = (unsigned char *) TMP_ALLOC (str_size);
81 /* Move the number to convert into temporary space, since mpn_get_str
82 clobbers its argument + needs one extra high limb.... */
83 xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB);
84 MPN_COPY (xp, x->_mp_d, x_size);
86 str_size = mpn_get_str (str, base, xp, x_size);
88 /* mpn_get_str might make some leading zeros. Skip them. */
95 /* Translate to printable chars. */
96 for (i = 0; i < str_size; i++)
97 str[i] = num_to_text[str[i]];
102 fwret = fwrite ((char *) str, 1, str_size, stream);
107 return ferror (stream) ? 0 : written;