C/C++ 16 进制 转 byte

2019-05-12  本文已影响0人  星星之火666

代码 (注意:字母全小写)

#include <iostream>
using namespace std;
#define byte unsigned char

byte hex2byte(char hexstr[]);

int main()
{
    char hex[3] = "67";
    cout << int(hex2byte(hex));
}

byte hex2byte(char hexstr[])
{
    byte h = hexstr[0];
    byte l = hexstr[1];
    if (isalpha(h))
        h = h - 'a' + 10;
    else
        h = h - '0';

    if (isalpha(l))
        l = l - 'a' + 10;
    else
        l = l - '0';

    return h * 16 + l;
}
上一篇 下一篇

猜你喜欢

热点阅读