0

I have been lately creating library managing big numbers. And when I say big I mean like hundred of digits and more. And here came a problem - I want to make it possible to pass the number as string and internally convert it into binary representation (array of unsigned char). It was quite simple for hexadecimal numbers. I did it like this:

void fillArrayfromHexadecimal(const std::string &hexadecimal, unsigned char *const array, const unsigned long long size){
    std::stringstream sstream { };
    for(unsigned long long i = 0; i < size; ++i){
        unsigned long long index {size >= 2 * i ? size - 2 * i : 0};
        unsigned char length { size >= 2 * i ? 2 : 1};
        sstream << std::hex << hexadecimal.substr(index, length);
        sstream >> array[i];
    }
}

And later array is processed by constructor.

But I stumbled into quite an issue - how to convert decimal in string to hexadecimal in string OR how to perform analogical conversion decimal in string -> char array? Any thoughts or ideas?

bartop
  • 9,971
  • 1
  • 23
  • 54
  • This might do it for you: http://stackoverflow.com/questions/4735622/convert-large-hex-string-to-decimal-string – NathanOliver Jun 26 '15 at 18:54
  • @NathanOliver I must say I should search more profoundly :/. I belive it should do it, thank you very much. – bartop Jun 26 '15 at 19:05

1 Answers1

-1

If you are only going to be storing the numbers, strings are adequate. But if you will be doing any arithmetic with them, you will probably need a class for dealing with large numbers. You can find an excellent article about that here.

Logicrat
  • 4,438
  • 16
  • 22