[英]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).
我只是想提取一个数字(0-9)或一个字母(AF),然后使用另一个函数将其转换为数字值,然后将这些值压入堆栈中(我本可以使用矢量,但是这次选择堆栈)。 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. 如下所示,正确的输出应为1728,但我得到的是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. 在您发布的代码的原始版本中,您将
0
和'0'
混合在一起,其他数字也一样。 The output was 12
because if you take 6C0
and ignore the 6
and 0
, the result is C
which is 12
. 输出为
12
因为如果采用6C0
并忽略6
和0
,则结果为C
,即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()
. 当您执行
pop()
,它将减小size()
。 So you only actually process half of the digits in the stack (rounded up). 因此,您实际上只处理堆栈中一半的数字(四舍五入)。 To fix this, loop until
myStack.size() == 0
instead. 要解决此问题,请循环直到
myStack.size() == 0
为止。
The problem is that the characters you're inputting aren't in the range of your if
statements. 问题在于您输入的字符不在
if
语句的范围内。 The digit characters don't have the values 0
- 9
, they are '0'
- '9'
- a different thing altogether. 数字字符没有值
0
- 9
,他们是'0'
- '9'
完全不同的事情- 。 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. 另外,您的
for
循环遍历了堆栈的大小-但是,随着您将内容从终端弹出,堆栈的大小正在改变。 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.