美文网首页
C/C++ 16 进制 转 byte

C/C++ 16 进制 转 byte

作者: 星星之火666 | 来源:发表于2019-05-12 02:57 被阅读0次

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

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

    相关文章

      网友评论

          本文标题:C/C++ 16 进制 转 byte

          本文链接:https://www.haomeiwen.com/subject/jtsqaqtx.html