title: C++ Hexadecimal to Decimal Conversion
body: <p>I currently have run into an issue that I seem to not understand even while debugging. I am trying to create a hexadecimal to decimal converter program, but I've run into a problem that I can't fix. Every time I try to input 8Fs or higher to convert into decimal, it ends up giving 16Fs' result (aka the max range:18446744....615)</p>
  
  <p>Power Function</p>
  
  <pre><code>int powerFunc(int x, int y)
  {
      unsigned long long int result = 1;
      int i;
  
      for (i = 0; i &lt; y; i++)
      {
          result *= x;
      }
  
      return result;
  }
  </code></pre>
  
  <p>Hex to Dec Function</p>
  
  <pre><code>int hexToDec(string hexaNumber)
  {
      int i;
      int power = hexaNumber.length() - 1;
      int checkLength = hexaNumber.length();
      unsigned long long int decimalResult = 0;
  
      for (i = 0; i &lt; checkLength; i++) {
          if (int(hexaNumber[i]) &gt;= '0' &amp;&amp; int(hexaNumber[i]) &lt;= '9') { // check if FirstHexaNumber 0 to 9
              decimalResult += ((int(hexaNumber[i])) - 48) * powerFunc(16, power); //formula to convert hexadecimal into decimal, int(FirstHexaNumber[i]) is used to convert hexa into a number
          }
          else if (int(hexaNumber[i]) &gt;= 'A' &amp;&amp; int(hexaNumber[i]) &lt;= 'F') // check if FirstHexaNumber is A to F
          {
              decimalResult += ((int(hexaNumber[i])) - 55) * powerFunc(16, power);
          }
          else if (int(hexaNumber[i]) &gt;= 'a' &amp;&amp; int(hexaNumber[i]) &lt;= 'f') // check if FirstHexaNumber is a to f
          {
              decimalResult += ((int(hexaNumber[i])) - 87) * powerFunc(16, power);
          }
          power--; //power-- since it starts from "HexaNumber.length - 1". Power should decrease as assignment of power goes down
      }
  
      return decimalResult;
  }
  </code></pre>
  
  <p>Main</p>
  
  <pre><code>int main()
  {
      unsigned long long int result;
      unsigned long long int dec1;
      unsigned long long int dec2;
      unsigned long long int totalDec;
      string hexa1;
  
      string FirstHexaNumber;
  
      getline(cin, FirstHexaNumber);
  
      dec1 = hexToDec(FirstHexaNumber);
  
      cout &lt;&lt; dec1 &lt;&lt; endl;
  
  
      return 0;
  }
  </code></pre>
  
  <p>I have tried debugging the issue, the power function works fine, it manages to get the result it should get. In this case, when I input "FFFFFFFF", the power I get is 268435456, which is correct.</p>
  
  <p>However, when it gets to the converting part, it messes up. For some reason, on the first run on the for loop for the formula, (int(F) - 55) * 16^8 = it ends up giving 18446744....615. 
  What can I do to solve this issue? Thanks in advance.</p>