Reputation: 10315
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