quietsamurai98 quietsamurai98 - 3 years ago 166
C++ Question

Convert an array of more than 64 bits to a base-10 string

I am trying to roll my own arbitrary size unsigned integer class, but I am having trouble finding a way to convert the

std::vector<bool>
I am using to store the bits into a base 10 string. Currently, my algorithm is

unsigned long long sum = 0LL;
for(unsigned long long i=0; i<64LL && i<bit_list.size(); i++){
if(bit_list[i]){
sum |= 1LL << i;
}
}
return std::to_string(sum);


However, this only works for numbers below 2^64, because of the limitations of
std::to_string
and the built in
unsigned long long
. How would I go about converting, a many bit unsigned int to a string representation of the base 10 conversion? Preferably, I would like the algorithm to be extendable to any number of bits.

Answer Source

Pseudo code:

string s;
your_bitlist_class i;
if ( i > 0 )
{
    while ( i > 0 )
    {
        s.insert( 0, std::to_string( i % 10 ) );
        i /= 10;   
    }
}
else
{
    s = "0";
}

Optimization is left as an exercise for the reader.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download