C语言编程(将十六进制字符串转换为十进制整数 )

2024-11-14 11:16:23
推荐回答(2个)
回答1:

number=0;
k=0;

for(i=0;s[i]!='\0';i++)
{

ch=s[i];
if(ch>='0'&&ch<='9'||ch>='a'&&ch<='f'||ch>='A'&&ch<='F')
num[k++]=ch;

}
num[k]='\0'
for(i=0 ;num[i]!='\0',i++)
{
ch=num[i];
if(ch>='0'&&ch<='9')
number=numbe*16+ch-'0';
else if(ch>='a'&&ch<='f')
number=number*16+ch-'a';
else if(ch>='A'&&ch<='F')
number=number*16+ch-'A';

}

回答2:

/* --- */ 部分:

i = 0; k = 0;
while(str[i] != 0)
{
ch = str[i];
if ( (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') || (ch >= '0' && ch <= '9') )
{
str[k] = ch;
++k;

}
++i;

}
str[k] = 0;

sscanf(str, "%x", &number);