Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 十六进制到十进制转换器不工作(c+;+;)_C++_Hex - Fatal编程技术网

C++ 十六进制到十进制转换器不工作(c+;+;)

C++ 十六进制到十进制转换器不工作(c+;+;),c++,hex,C++,Hex,所以,作为作业的一部分,我写了一个程序将十六进制转换成十进制。但我没能得到想要的结果。有人能指出这个代码中的错误吗 #include<bits/stdc++.h> #include<math.h> using namespace std; int hexaToDecimal(string n){ int ans = 0; int power =1; int s = n.size(); for(int i=s-1; i>=0; i-

所以,作为作业的一部分,我写了一个程序将十六进制转换成十进制。但我没能得到想要的结果。有人能指出这个代码中的错误吗

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

int hexaToDecimal(string n){
    int ans = 0;
    int power =1;
    int s = n.size();

    for(int i=s-1; i>=0; i--){
        if(n[i] >= '0' && n[i] <= '9'){
            ans = ans + power*(n[i]);
        }
        else if(n[i] >= 'A' && n[i] <= 'F'){
            ans = ans + power*(n[i]-'A' + 10);
        }
        power = power * 16;
    }
    return ans;
}

        


int main(){
    string n;
    cin>>n;
    cout<<hexaToDecimal(n)<<endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int十六进制(字符串n){
int ans=0;
整数幂=1;
int s=n.size();
对于(int i=s-1;i>=0;i--){
如果(n[i]>='0'和&n[i]='A'和&n[i]>n;

cout您可以像添加
-'A'
一样添加
-'0'
。 代码如下:

#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int hexaToDecimal(string n){
    int ans = 0;
    int power =1;
    int s = n.size();

    for(int i=s-1; i>=0; i--){
        if(n[i] >= '0' && n[i] <= '9'){
            ans = ans + power*(n[i] - '0'); //THERE.
        }
        else if(n[i] >= 'A' && n[i] <= 'F'){
            ans = ans + power*(n[i]-'A' + 10);
        }
        power = power * 16;
    }
    return ans;
}

        


int main(){
    string n;
    cin>>n;
    cout<<hexaToDecimal(n)<<endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int十六进制(字符串n){
int ans=0;
整数幂=1;
int s=n.size();
对于(int i=s-1;i>=0;i--){
如果(n[i]>='0'和&n[i]='A'和&n[i]>n;

cout关于如何解决您的问题,这里有一种不同的方法。您可以使用headerfile
#include
中定义的
std::hex
,这是一种更简单的方法

例如:

#include <iostream>


int main()
{
    int HexNum;
    std::cin >> std::hex >> HexNum;
    std::cout << HexNum << std::endl;

    return 0;
}

更简单的方法是:

unsigned fromHex(const string &s) { 
    unsigned result = 0;
    for (char c : s) 
        result = result << 4 | hexDigit(c);
    return result;
}

unsigned hexDigit(char c) { 
    return c > ‘9’ ? c - ‘A’ + 10: c - ‘0’;
}
unsigned fromHex(常量字符串&s){
无符号结果=0;
用于(字符c:s)
结果=结果“9”?c-“A”+10:c-“0”;
}

<代码> ANS= ANS+POWER *(n[i]);
=<代码> ANS= ANS+POWER *(n[i] -' 0′);
;考虑不使用,是的,它现在起作用,但我不能得到它背后的逻辑。当使用“0”或“a”类int时,不是‘0’=0,它是‘0’=48(ASCII码)。例如,你有'5'。'5'=53,'0'=48,'5'-'0'=53-48=5。A、B、C等也是一样。你的
toDec
返回
1
,如果你用
B
来称呼它。这很好,但有点违背了作业的目的,你不认为吗?@Kostas-是和否。我认为这是一个非常甜蜜的回答,因为它利用了什么在头文件中,它很简单,直截了当。我也喜欢你的答案。
unsigned fromHex(const string &s) { 
    unsigned result = 0;
    for (char c : s) 
        result = result << 4 | hexDigit(c);
    return result;
}

unsigned hexDigit(char c) { 
    return c > ‘9’ ? c - ‘A’ + 10: c - ‘0’;
}