Handle arbitrary base number systems

Marc Glisse marc.glisse at inria.fr
Mon May 23 11:43:37 CEST 2011

On Sun, 22 May 2011, Flavio Andreazzi wrote:

> Dear GMP community,
> I would like to know whether it is possible to handle number systems for
> (natural) bases grater than 62 through GMP.
> For instance is it possible to represent a bit-string of arbitraty length as
> a string of digits in base 103, work with this and finally convert the
> string back to binary?
> I have read the manual and some old threads, but the only function available
> seems to be
> char * mpz_get_str (char *str, int base, mpz_t op)
> [at http://gmplib.org/manual/Converting-Integers.html]
> with base either in the range [2,..,62] or [-2,..,-36]

There is also the lower-level mpn_get_str that works with base 2-256.

> It would be nice to have a function which allows "conversion" from an
> arbitrary base b0 to another b1. Can this be achieved using GMP?

If you don't care about performance, it is easy to recursively divide by 
base (and the larger the base, the less cleverer algorithms would help). 
If you do care, you can look at the code for mpn_get_str. It looks like 
any base that fits in a limb won't be too hard to handle.

Marc Glisse

More information about the gmp-discuss mailing list