user142019
user142019

Reputation:

Printing a base 4294967296 integer in base 10

I have in C++ a vector of 32 bits integers (variable size, continous memory; like a C-array), representing a number in base 4294967296. I would like to print it in base 10.

These numbers can be extremely big and take over a few megabytes of memory.

What would be the best way to do this in terms of performance? Can I use GMP to do this?

Upvotes: 10

Views: 842

Answers (1)

Mysticial
Mysticial

Reputation: 471249

Yes, you can use GMP for this. The function that you're looking for is mpn_get_str:

http://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Functions

Now the only issue is the size of mp_limb_t. It is either a 32-bit integer or a 64-bit integer depending on the platform.

  • If it's a 32-bit integer, then you can call the function directly on your array of 32-bit integers. (if the endian matches)
  • If it's a 64-bit integer, you may be able to still use it with just a pointer cast. (depending on the alignment and the endianness) Otherwise, you'll have to copy your array into an array of 64-bit integers before you can call mpn_get_str.

Alternatively, it might be easier to use the mpz integer class. Import your integer array into a large integer, then print it back out in base 10.

Upvotes: 7

Related Questions