bartop
bartop

Reputation: 10315

Hexadecimal in string to decimal in string and vice versa

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?

Upvotes: 0

Views: 106

Answers (1)

Logicrat
Logicrat

Reputation: 4468

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.

Upvotes: -1

Related Questions