16进制数转换为10进制---用的数组

#include<stdio.h>  

static int hex_table[] = { 0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,1,2,3,4,5,6,
7,8,9,0,0,0,0,0,0,
0,10,11,12,13,14,15,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,10,
11,12,13,14,15 };
char hex_to_decimal(char* hex_str)
{
    char ch;
    int iret = 0;
    while (ch = *hex_str++)
    {
        iret = (iret << 4) | hex_table[ch];
    }
    return iret;
}
int main()
{
    char s[50];
    printf("请输入16进制数:");
    scanf("%s", s);
    
    unsigned char sum = hex_to_decimal(s);


    printf("转化为10进制数为:%ld\n", sum);
    while (1)
    {;
    }

    return 0;
}

 

上一篇:浅谈Linux操作系统


下一篇:多级指针使用分割字符串