Conversion between numerical bases going wrong
<p>I don't have a clue on what is wrong with my program. I just went testing if this would be usable on at least a console application before adding this "experiment"to my project and as you can see I failed. </p>

<p>I made a library that converts a number between numerical bases (so far, only B16 or B2 to decimal). It's a big work-around since I couldn't understand very well how to manage modulus and divisions, but it seemingly worked until a point. </p>

<p>I gave it a 0xFF and it treated it as 0xFF0, the result being 4,080. I gave it a 0x16 and it treated it like a 0x160, the result being 352. It's obvious what is happening, but I don't know why it is. At least, so far, the problem is only being with B16, although B2 did gave me some weird errors as well.</p>

<p>Here is the code (Code::Blocks on GNU compiler):</p>

<pre><code>int bin_to_int(char char_digit){
    char_digit = toupper(char_digit);
         if (char_digit == '0') {return 0;}
    else if (char_digit == '1') {return 1;}
    else {return 2;}
}
int hex_to_int(char char_digit){
    char_digit = toupper(char_digit);
         if (char_digit == '0') {return 0;}
    else if (char_digit == '1') {return 1;}
    else if (char_digit == '2') {return 2;}
    else if (char_digit == '3') {return 3;}
    else if (char_digit == '4') {return 4;}
    else if (char_digit == '5') {return 5;}
    else if (char_digit == '6') {return 6;}
    else if (char_digit == '7') {return 7;}
    else if (char_digit == '8') {return 8;}
    else if (char_digit == '9') {return 9;}
    else if (char_digit == 'A') {return 10;}
    else if (char_digit == 'B') {return 11;}
    else if (char_digit == 'C') {return 12;}
    else if (char_digit == 'D') {return 13;}
    else if (char_digit == 'E') {return 14;}
    else if (char_digit == 'F') {return 15;}
    else {return 16;}
}
void conv_todec(int mode_base){
    if (mode_base == 16){std::cout &lt;&lt; "\nHex to Dec function";}
    if (mode_base ==  2){std::cout &lt;&lt; "\nBin to Dec function";}
    if (mode_base == 16){std::cout &lt;&lt; "\nEnter a hex number: ";}
    if (mode_base ==  2){std::cout &lt;&lt; "\nEnter a bin number: ";}
    std::string numb;
    std::cin &gt;&gt; numb;
    int str_leg = numb.length();
    int total = 0;
    for(int i(0);i&lt;str_leg;i++){
        if (mode_base == 16){
            if (hex_to_int(numb[i]) == 16){func_erro();return;}}
        if (mode_base == 2){
            if (bin_to_int(numb[i]) == 2){func_erro();return;}}
        }
    int digits[str_leg] = {};
    int j = str_leg;
    for(int i(0);i&lt;str_leg;i++){
    if (mode_base == 16) {digits[i] = hex_to_int(numb.at(i));}
    if (mode_base ==  2) {digits[i] = bin_to_int(numb.at(i));}
    if (mode_base == 16) {total += (digits[i] * power(mode_base,j--));}
    //Somehow the number never had the first digit claiming it as
    //either even or odd, so I tried pre-decrementing 'j' and it
    //worked for some reason I don't know either.
    if (mode_base ==  2) {total += (digits[i] * power(mode_base,--j));}}
    std::cout &lt;&lt; "The result is: " &lt;&lt; total &lt;&lt; std::endl;
}
</code></pre>