Construction From vector<char> ?

Marc Glisse marc.glisse at
Wed Aug 13 19:38:49 CEST 2008

On Wed, 13 Aug 2008, Robert Evans wrote:

> Also very useful for me would be the output of an mpz_class object into
> an array (vector<char>).  Perhaps there is a way to do this via the
> ostream operators?  My basic problem is:  we are already using byte
> arrays which represent trans-64 bit numbers.  I could do a HUGE amount of
> refactoring of really bad code which manipulates such arrays if I could
> construct a mpz_class from such an array, do the arithmetic within GMP,
> output back into an array.   The C++ interface seems to character-based
> I/O only.

I am having a hard time understanding what you say. You are complaining 
that the interface is character-based and you would like something that 
interacts better with a vector of characters...

If as I believe your vector<char> is just a string with a silly name, the 
conversions are pretty trivial, and I gave you two ways (there are many 
others) to go from vector<char> to string. The reverse direction is just 
as easy.

The only other thing your vector could be is like a gmp representation 
with 8-bit limbs, which sounds very strange so I am not considering it.

If you think the conversions include too many copies, you can use the 
mpn_set_str function for instance, that takes a char* and a length instead 
of expecting a null char to mark the end. But beware that you need to deal 
with the allocations yourself then.

Marc Glisse

More information about the gmp-discuss mailing list