1 /* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string
2 with the result of the conversion.
4 Copyright (C) 1991, 1994 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 Library General Public License as published by
10 the Free Software Foundation; either version 2 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 Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library 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. */
36 mp_size_t xsize = x->_mp_size;
38 unsigned char *str, *s;
46 str = (unsigned char *) (*_mp_allocate_func) (2);
56 str_size = ((size_t) (xsize * BITS_PER_MP_LIMB
57 * __mp_bases[16].chars_per_bit_exactly)) + 3;
58 str = (unsigned char *) (*_mp_allocate_func) (str_size);
64 /* Move the number to convert into temporary space, since mpn_get_str
65 clobbers its argument + needs one extra high limb.... */
66 xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB);
67 MPN_COPY (xp, x->_mp_d, xsize);
69 str_size = mpn_get_str (s, 16, xp, xsize);
71 /* mpn_get_str might make some leading zeros. Skip them. */
72 for (zeros = 0; s[zeros] == 0; zeros++)
75 /* Translate to printable chars and move string down. */
76 for (i = 0; i < str_size; i++)
77 s[i] = "0123456789abcdef"[s[zeros + i]];