Converting to Base 256

Ken Smith kgsmith at gmail.com
Wed Dec 13 20:47:18 CET 2006


On 12/13/06, Paul Zimmermann <Paul.Zimmermann at loria.fr> wrote:
> > Is there any way GnuMP can make what I'm trying to do as easy as I
> > thought it might be?
>
> You might want to use mpn_get_str(). It can deal with base 256 according to the
> documentation, but is more tricky to use.

Thank you very much for the suggestion, Paul.  I looked into
mpn_get_str and, just as I was about to figure out how to get branches
from an mpz_t, I stumbled across mpz_export.  This function does
exactly what I need.

For the curious, the following amends my naïve example accomplishing
my original goal.

1: const char* line = "1a2b3c90effe487db13f45c9872b309e47234a";
2:
3: mpz_t line_as_num;
4: mpz_init_set_str(line_as_num, line, 16);
5:
6: size_t line_as_bytes_len = 0;
7: void* line_as_bytes =
8:    mpz_export(NULL, &line_as_bytes_len, 1, 1, 1, 0, line_as_num);
...
n-1: free(line_as_bytes)
n: mpz_clear(line_as_num);

  Thanks again!
  Ken Smith


More information about the gmp-discuss mailing list