![](https://img-blog.csdnimg.cn/direct/0eb741023a3e48c9aab5beea002d2a17.png)
一般写法:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
string str;
while(cin >> str)
{
int sign=1;
if(str[0]=='-')
{
sign=-1;
str=str.substr(1);
}
str=str.substr(2);
int res=0;
for(auto c:str)
{
int t;
if(c>='A'&&c<='F')t=c-'A'+10;
else if(c>='a'&&c<='f')t=c-'a'+10;
else t=c-'0';
res=res*16+t;
}
cout << res*sign << endl;
}
return 0;
}
公式写法:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int x;
while(cin >> hex >> x)
{
cout << x << endl;//hex表示会默认输出16为进制数转为十进制数的结果
}
return 0;
}