十六进制数转换为十进制

/*
由于发过来的数据为16进制,故需要把16进制的数据变为10进制,然后由Mat进行保存
*/
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 };
unsigned char hex_to_decimal(char* hex_str)
{
    char ch;
    unsigned char iret = 0;
    while (ch = *hex_str++)
    {
        iret = (iret << 4) | hex_table[ch];
    }
    return iret;
}
posted @ 2019-08-09 13:41  盖世猪猪侠  阅读(847)  评论(0编辑  收藏  举报