version 1.1.1.2, 2000/09/09 14:13:17 |
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, 2000 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. |
|
|
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 (char *) 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; |
|
|
|
ASSERT (strlen (str) + 1 == alloc_size); |
return (char *) str; |
return (char *) str; |
} |
} |