输入案例:±c2#
输出结果-194
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
char s1[1000];
char s2[1000];
int a = 0;
int call(int a)//将每位十六进制符号转化为十进制
{
int outcome = 0;
if (a >= '0' && a <= '9')
{
outcome = a - 48;
}
else if ((a >= 'A' && a <= 'F'))
{
outcome = a - 55;
}
else
outcome = a - 87;
return outcome;
int main()
{
int keys = 0;
int n = 0;//确定是否有负号
printf("请输入一个字符串:\n");
gets_s(s1);//读入一串字符
for (int i = 0,j=0;i < strlen(s1); i++)
{
if ((s1[i] >= '0' && s1[i] <= '9') || (s1[i] >= 'A' && s1[i] <= 'F') || (s1[i] >= 'a' && s1[i] <= 'f'))
{
s2[j] = s1[i];
j++;
if (j == 1)
n++;
}
else
continue;
}//过滤掉其他字符
printf("原始数组s1=%s\n", s1);
printf("过滤后的数组s2=%s\n", s2);
for (int i = strlen(s2)-1,k=0; i>=0; i--,k++)//倒着来的
{
keys = keys + ((call(s2[i])) * pow(16, k));
}
if (n == 1)
printf("该十六进制值为:\n-");
printf("%d", keys);
system("pause");
return 0;
}
版权声明:本文为weixin_63676550原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。