1
/* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string
2
with the result of the conversion.
4
Copyright 1991, 1994, 2000, 2001, 2002 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. */
31
mp_size_t xsize = x->_mp_size;
33
unsigned char *str, *s;
40
str = (unsigned char *) (*__gmp_allocate_func) (2);
50
str_size = ((size_t) (xsize * BITS_PER_MP_LIMB
51
* __mp_bases[16].chars_per_bit_exactly)) + 3;
52
str = (unsigned char *) (*__gmp_allocate_func) (str_size);
58
/* Move the number to convert into temporary space, since mpn_get_str
59
clobbers its argument + needs one extra high limb.... */
60
xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB);
61
MPN_COPY (xp, x->_mp_d, xsize);
63
str_size = mpn_get_str (s, 16, xp, xsize);
65
/* mpn_get_str might make some leading zeros. Skip them. */
66
for (zeros = 0; s[zeros] == 0; zeros++)
69
/* Translate to printable chars and move string down. */
70
for (i = 0; i < str_size; i++)
71
s[i] = "0123456789abcdef"[s[zeros + i]];