Using C++, how to convert a string of one base to a string of another base without integer limitations?

Viewed 50

I am looking for a way to convert a "binary" or "hex" or "octal" string to "binary" or "hex" or "octal". I was able to do an INTEGER approach where I am limited by the value bounded to 2^31 - 1 or something.

Here is my code framework:

// https://codescracker.com/cpp/program/cpp-program-convert-octal-to-binary.htm
// https://ubuntuforums.org/showthread.php?t=739716
std::string s_base2base(std::string s, int from=16, int to=2)
    {
    // from (2,32) ... to (2,32)
    std::string d = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
    
    std::string res;
    
    // do something here ... 
    
    return res;
    }

Here are the working INTEGER functions:

std::string s_int2base(long long int num, int base=16)
    {
    std::string d = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
    std::string res;
    if(num == 0) { return "0"; }
    while(num > 0)
        {
        res = d[num % base] + res;
        num /= base;
        }
    return res;
    }


long long int s_base2int(std::string s, int base=16)
    {   
    long long int res = std::stoi( s, 0, base );
    return res;
    }

Question: Using C++, how to convert a string of one base to a string of another base without integer limitations?

The examples are friendly bases, but I would like a variable function where from and to are in the range 2,32

0 Answers
Related