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;
}

 

posted @ 2019-08-06 15:45  盖世猪猪侠  阅读(1021)  评论(0编辑  收藏  举报