summaryrefslogtreecommitdiff
path: root/mpbsd/mtox.c
blob: 7babfdd07fc43be689331370cb38d64422e416f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* mtox -- Convert OPERAND to hexadecimal and return a malloc'ed string
   with the result of the conversion.

Copyright 1991, 1994, 2000, 2001, 2002 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

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
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

#include <string.h>
#include "mp.h"
#include "gmp.h"
#include "gmp-impl.h"
#include "longlong.h"

char *
mtox (const MINT *x)
{
  mp_size_t xsize = x->_mp_size;
  mp_ptr    xp;
  mp_size_t xsign;
  unsigned char *str, *s;
  size_t str_size, alloc_size, i;

  xsign = xsize;
  if (xsize < 0)
    xsize = -xsize;

  /* digits, plus '\0', plus possible '-', for an exact size */
  xp = x->_mp_d;
  MPN_SIZEINBASE_16 (alloc_size, xp, xsize);
  alloc_size += 1 + (xsign < 0);

  str = (unsigned char *) (*__gmp_allocate_func) (alloc_size);
  s = str;

  if (xsign < 0)
    *s++ = '-';

  str_size = mpn_get_str (s, 16, xp, xsize);
  ASSERT (str_size <= alloc_size - (xsign < 0));
  ASSERT (str_size == 1 || *s != 0);

  for (i = 0; i < str_size; i++)
    s[i] = "0123456789abcdef"[s[i]];
  s[str_size] = 0;

  ASSERT (strlen (str) + 1 == alloc_size);
  return (char *) str;
}