Construction From vector<char> ?

David Gillies daggillies at gmail.com
Thu Aug 14 00:31:37 CEST 2008


mpz_import() will indeed do the trick if you have a big chunk of
bytes, and it can even get round endianness issues. The problem you
have is that C++ makes no pledges about the in-memory representation
of container types. A vector<char> may well use a member of type char*
to store a flat array of its contents, but nothing says it has to, and
nothing says you can get at it anyway without doing something evil.
Further, even if you have access to the raw data as a pointer to an
array, you can't just simply reinterpret_cast the pointer to be
mp_limb_t* (at least not portably - there's no ironclad law that says
a long is four bytes in a row, for example, and you'll probably bus
fault on unaligned data on a lot of machines). So mpz_import() will be
stuck doing a bytewise import.


On Wed, Aug 13, 2008 at 3:31 PM, Marc Glisse <marc.glisse at normalesup.org> wrote:
> On Wed, 13 Aug 2008, Robert Evans wrote:
>
>> Sorry, I am not explaining myself well.  What we have is not a vector
>> containing the characters (i.e. ascii) representing a number, but the
>> actual hex bytes.  For example, we might have a 4 byte array containing
>> [0x31 0x32 0x33 0x34].  For us this is NOT the number "1234" but the
>> number 0x31323334 or in decimal 825373492.  I supposed we should have
>> used the name vector<unsigned short> but someone used vector<char> which
>> is a bit  misleading.
>
> Wow ok. The most confusing thing is that the smallest the type you put in
> vector, the slower the computations on that number will be. I just
> couldn't believe that someone would use char there...
>
> Then what you are looking for is most likely mpz_import/mpz_export.
>
> --
> Marc Glisse
> _______________________________________________
> gmp-discuss mailing list
> gmp-discuss at swox.com
> https://gmplib.org/mailman/listinfo/gmp-discuss
>



-- 
David Gillies
San Jose
Costa Rica


More information about the gmp-discuss mailing list