version 1.1.1.1, 2000/01/10 15:35:22 |
version 1.1.1.3, 2003/08/25 16:06:37 |
|
|
/* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string |
/* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string |
with the result of the conversion. |
with the result of the conversion. |
|
|
Copyright (C) 1991, 1994 Free Software Foundation, Inc. |
Copyright 1991, 1994, 2000, 2001, 2002 Free Software Foundation, Inc. |
|
|
This file is part of the GNU MP Library. |
This file is part of the GNU MP Library. |
|
|
The GNU MP Library is free software; you can redistribute it and/or modify |
The GNU MP Library is free software; you can redistribute it and/or modify |
it under the terms of the GNU Library General Public License as published by |
it under the terms of the GNU Lesser General Public License as published by |
the Free Software Foundation; either version 2 of the License, or (at your |
the Free Software Foundation; either version 2.1 of the License, or (at your |
option) any later version. |
option) any later version. |
|
|
The GNU MP Library is distributed in the hope that it will be useful, but |
The GNU MP Library is distributed in the hope that it will be useful, but |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
License for more details. |
License for more details. |
|
|
You should have received a copy of the GNU Library General Public License |
You should have received a copy of the GNU Lesser General Public License |
along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
MA 02111-1307, USA. */ |
MA 02111-1307, USA. */ |
Line 23 MA 02111-1307, USA. */ |
|
Line 23 MA 02111-1307, USA. */ |
|
#include "mp.h" |
#include "mp.h" |
#include "gmp.h" |
#include "gmp.h" |
#include "gmp-impl.h" |
#include "gmp-impl.h" |
|
#include "longlong.h" |
|
|
char * |
char * |
#if __STDC__ |
|
mtox (const MINT *x) |
mtox (const MINT *x) |
#else |
|
mtox (x) |
|
const MINT *x; |
|
#endif |
|
{ |
{ |
mp_ptr xp; |
|
mp_size_t xsize = x->_mp_size; |
mp_size_t xsize = x->_mp_size; |
|
mp_ptr xp; |
mp_size_t xsign; |
mp_size_t xsign; |
unsigned char *str, *s; |
unsigned char *str, *s; |
size_t str_size, i; |
size_t str_size, alloc_size, i; |
int zeros; |
|
char *num_to_text; |
|
TMP_DECL (marker); |
|
|
|
if (xsize == 0) |
|
{ |
|
str = (unsigned char *) (*_mp_allocate_func) (2); |
|
str[0] = '0'; |
|
str[1] = 0; |
|
return str; |
|
} |
|
xsign = xsize; |
xsign = xsize; |
if (xsize < 0) |
if (xsize < 0) |
xsize = -xsize; |
xsize = -xsize; |
|
|
TMP_MARK (marker); |
/* digits, plus '\0', plus possible '-', for an exact size */ |
str_size = ((size_t) (xsize * BITS_PER_MP_LIMB |
xp = x->_mp_d; |
* __mp_bases[16].chars_per_bit_exactly)) + 3; |
MPN_SIZEINBASE_16 (alloc_size, xp, xsize); |
str = (unsigned char *) (*_mp_allocate_func) (str_size); |
alloc_size += 1 + (xsign < 0); |
|
|
|
str = (unsigned char *) (*__gmp_allocate_func) (alloc_size); |
s = str; |
s = str; |
|
|
if (xsign < 0) |
if (xsign < 0) |
*s++ = '-'; |
*s++ = '-'; |
|
|
/* Move the number to convert into temporary space, since mpn_get_str |
|
clobbers its argument + needs one extra high limb.... */ |
|
xp = (mp_ptr) TMP_ALLOC ((xsize + 1) * BYTES_PER_MP_LIMB); |
|
MPN_COPY (xp, x->_mp_d, xsize); |
|
|
|
str_size = mpn_get_str (s, 16, xp, xsize); |
str_size = mpn_get_str (s, 16, xp, xsize); |
|
ASSERT (str_size <= alloc_size - (xsign < 0)); |
|
ASSERT (str_size == 1 || *s != 0); |
|
|
/* mpn_get_str might make some leading zeros. Skip them. */ |
|
for (zeros = 0; s[zeros] == 0; zeros++) |
|
str_size--; |
|
|
|
/* Translate to printable chars and move string down. */ |
|
for (i = 0; i < str_size; i++) |
for (i = 0; i < str_size; i++) |
s[i] = "0123456789abcdef"[s[zeros + i]]; |
s[i] = "0123456789abcdef"[s[i]]; |
s[str_size] = 0; |
s[str_size] = 0; |
|
|
return str; |
ASSERT (strlen (str) + 1 == alloc_size); |
|
return (char *) str; |
} |
} |