简体   繁体   中英

C++: Hex to Decimal Conversion Function - Not getting correct output

I'm trying to convert hex values to decimal. I'm just trying to extract either a number (0-9) or letter (AF), which I'm converting to the number value with another function, and then push the values into a stack (I could have used vector, but chose stack this time). I'm then just trying to get the decimal value, and since a stack works backwards, it should be in order.

The correct output, as shown below, should be 1728, but I'm getting 12.

Thanks in advance.

#include <iostream>
#include <string>
#include <math.h>
#include <stack>

using namespace std;

int hexCharToDec(char a);
void hexToDecimal(string str, stack<int> & myStack);

int main(){

    stack<int> myStack;
    string str = "6C0"; // should be 1728
    hexToDecimal(str, myStack);

}

void hexToDecimal(string str, stack<int> & myStack){

    int totalVal = 0;

    for (int i = 0; i < str.length(); i++){

        if (str.at(i) <= 9)
        myStack.push(str.at(i));

        else if (str.at(i) >= 'A' && str.at(i) <= 'F')
        myStack.push(hexCharToDec(str.at(i)));
    }

    int k = 0;

    for (int i = 0; i < myStack.size(); i++){
      totalVal += (myStack.top() * pow(16, k++));

      myStack.pop();

    }
    cout << totalVal;
}

int hexCharToDec(char hexChar){

  switch(hexChar){

      case 'A':
      return 10;
      break;

      case 'B':
      return 11;
      break;

      case 'C':
      return 12;
      break;

      case 'D':
      return 13;
      break;

      case 'E':
      return 14;
      break;

      case 'F':
      return 15;
      break;
}}

In the original version of the code you posted, you mixed up 0 with '0' and likewise for the other digits. The output was 12 because if you take 6C0 and ignore the 6 and 0 , the result is C which is 12 .

In the updated version this loop is broken:

for (int i = 0; i < myStack.size(); i++)
{
    totalVal += (myStack.top() * pow(16, i));
    myStack.pop();
}

When you do pop() , it reduces size() . So you only actually process half of the digits in the stack (rounded up). To fix this, loop until myStack.size() == 0 instead.

The problem is that the characters you're inputting aren't in the range of your if statements. The digit characters don't have the values 0 - 9 , they are '0' - '9' - a different thing altogether. Those characters don't get processed at all.

In addition, your for loop is looping over the size of the stack - but the size of the stack is changing as you pop things off the end. You're only processing half of the input.

A working solution:

#include<iostream>
#include <string>
#include <sstream>
int main()
{
    int n;
    string str = "6C0";
    istringstream sin(str);
    sin>>hex>>n;
    cout<<n<<endl;
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM