I'm working on a program and I need to convert big numbers to radix 64.
I would like to shorter them whit conversion that's why I choosed base 64.
I have two problems:
1. The conversion works only for not so big numbers. Untill about 2^32.
I would like to convert bigger numbers. Is there any solution?
(I thought on GMP/MPIR library, but I can't managed it.)
2. The conversion back to decimal base doesn't works, because I use 'strtoul()' which doesn't support bigger bases like 36.
Is there any good method for that?
The mpz_set_str gives value for the variable. It reads the value what I want to give from a file to inputStr.
The inputStr must be declared like:
char inputStr[1500];
The problem is that my input file can be different size. So I can't set a concret number for elements of array. If I set it to a very big element number I get an error when I run. (with compiling isn't there any problem)
A good alternative would be a vector. I tried it, but I get an error.
Is there any way to use mpz_set_str with a vector? Or any other solution?
STL containers allow direct (pointer) access to the underlying data. So you could use a std::string to hold the text version and pass it to GMP with the .c_str() member function. (Check out the Reference link in the top-right of this page.) Likewise, you can get an array from a std::vector by using the .data() member function.
Just remember, these things are not supposed to be mutable...
Also, you should be able to adjust the size of a number in GMP. I don't remember the function to do it, but... read the docs.
> mpz_set_str(value,inputStr, 10);
> The mpz_set_str gives value for the variable. It reads the value what I want to give from a file to inputStr.
> The inputStr must be declared like: char inputStr[1500];
> The problem is that my input file can be different size.
Use std::string in conjunction with mpz_set_str() . For instance:
This code can convert decimal numbers to radix 62. It works betwenn 2-62.
What could I do If I would like to convert it to very high base? For example base 512 or 1024.
What do you thin is it possible? Because If I want to convert it to base 512 I need 512 different character for represent numbers.
Different 'bases' are for humans (so that we can read the numbers).
Once you get up to base 256 then you are using one byte's worth of bits for each 'digit' -- which is the same as saying your number is (1) not human readable and (2) binary storage (because it's an array of bytes).
The GMP stores numbers in arrays of long ints (IIRC), so you could just write that information straight to a binary file.
For so many digits, don't bother to try to use human-readable characters. You would have to start playing with Unicode or Shift-JIS just to do that, which would take more space to keep the number than just the binary data, and humans wouldn't be able to read it anyway.
Hm, I don't know.
I use base 62 at the moment.
The input number is decimal and 4194304 digit long! In base 62 it is just 2340060 digit (4096 kb and 2286 kb).
#include <iostream>
#include <fstream>
#include <iomanip>
int main()
{
std::ifstream file(__FILE__) ;
std::string str ;
char c ;
// read characters (including whitespace) up to a delimiter
std::getline( file, str, '&' ) ;
std::cout << str << "\n\n" ;
// skip leading white space, read a maximum of 15 characters or till a whitespace
file >> std::setw(15) >> str ;
std::cout << str << "\n\n" ;
// do not skip leading white space, read a maximum of 25 characters (including whitespace) or eof
str.clear() ;
while( str.size() < 25 && file.get(c) ) str += c ;
std::cout << str << "\n\n" ;
// read a maximum of 30 characters (excluding whitespace) or eof
str.clear() ;
while( str.size() < 30 && file >> c ) str += c ;
std::cout << str << "\n\n" ;
}