Converting the number to bases above 62

Paul Zimmermann Paul.Zimmermann at
Sun Sep 23 18:54:28 CEST 2007

> Date: Sun, 23 Sep 2007 10:45:39 -0300
> From: "=?ISO-8859-1?Q?Ars=E8ne_Lupin?=" <detentor at>
> Hi.
> I'd like to convert an mpz_class or mpz_t number to a base 256, for it
> to fill a byte vector.
> But all the convertion functions only work until base 62. Any of you know
> another way of converting the base? My real problem is that I wanted to save
> the number in a file, but I realized that the GMP, even for integer (which
> is my application uses - only natural numbers, not signed), somewhat writes
> to the file (using mpz_out_raw) more than the bytes needs to be stored. Even
> though the difference is about bytes, its critical to my project that it
> writes to the file only the necessary bytes. Any ideas that could help me?
> Thanks in advance.
> Detentor

Since 256=16^2, I would convert to base 16 using mpz_get_str, and then
convert two consecutive base-16 characters to base 256.

Paul Zimmermann

More information about the gmp-discuss mailing list