写出一个程序,接受一个十六进制的数值字符串,输出该数值的十进制字符串。(多组同时输入 ) 输入描述: 输入一个十六进制的数值字符串。 输出描述: 输出该数值的十进制字符串。 输入例子: 0xA 输出例子: 10 #include<iostream> #include<string> using namespace std; int main() { int a; while (cin >> hex >> a)//十六进制输入 { cout << a << endl;//十进制输出 } return 0; } 第二种 #include<stdio.h>
#include<string.h>
#include<math.h>
int
main()
{
char
str[100];
int
i=0,count,sum;
while
(
gets
(str))
//用于多次输入
{
count=
strlen
(str);
//计算字符串的长度
sum=0;
for
(i=count-1;i>=0;i--)
//从十六进制个位开始,每位都转换成十进制
{
if
(str[i]>=
'0'
&&str[i]<=
'9'
)
//数字字符的转换
{
sum+=(str[i]-48)*
pow
(16,count-i-1);
}
else
if
(str[i]>=
'A'
&&str[i]<=
'F'
)
//字母字符的转换
{
sum+=(str[i]-55)*
pow
(16,count-i-1);
}
}
printf
(
"%d\n"
,sum);
}
return
0;
}
|
华为5进制转换
最新推荐文章于 2023-05-10 22:03:47 发布