zoukankan      html  css  js  c++  java
  • C/C++ 16进制转字符串,字符串转16进制 EX

    {

    int Encryption::HexToStr(char *Hex)
    {
        int _0 = 0;
        int _1 = 0;
        char buf[2] = { 0 };
        if (Hex[0] >= '0' && Hex[0] <= '9')
        {
            buf[0] = Hex[0];
            _0 = atoi(buf);
        }
        else
        {
            switch (Hex[0])
            {
            case 'A':
            {
                _0 = 10;
            }; break;
            case 'B':
            {
                _0 = 11;
            }; break;
            case 'C':
            {
                _0 = 12;
            }; break;
            case 'D':
            {
                _0 = 13;
            }; break;
            case 'E':
            {
                _0 = 14;
            }; break;
            case 'F':
            {
                _0 = 15;
            }; break;
            }
    
        }
        if (Hex[1] >= '0' && Hex[1] <= '9')
        {
            buf[0] = Hex[1];
            _1 = atoi(buf);
        }
        else
        {
            switch (Hex[1])
            {
            case 'A':
            {
                _1 = 10;
            }; break;
            case 'B':
            {
                _1 = 11;
            }; break;
            case 'C':
            {
                _1 = 12;
            }; break;
            case 'D':
            {
                _1 = 13;
            }; break;
            case 'E':
            {
                _1 = 14;
            }; break;
            case 'F':
            {
                _1 = 15;
            }; break;
            }
    
        }
    
        return _0 * 16 + _1;
    }
    
    char *Encryption::StringToHex_s(char *String, int Inlen)
    {
        if (String == NULL || String == nullptr) { return nullptr; }
    
        int BufLen = Inlen * 2 + 1;
    
        char *Buf = new char[BufLen];
        memset(Buf, 0, BufLen);
    
        char buf[3] = { 0 };
    
        for (int i = 0; i < Inlen; i++)
        {
            sprintf_s(buf, "%02X", (unsigned char)String[i]);
            (Buf + (i * 2))[0] = buf[0];
            (Buf + (i * 2))[1] = buf[1];
        }
        return Buf;
    }
    
    char *Encryption::StringToHex_s(const char *String, int Inlen)
    {
        return Encryption::StringToHex_s((char*)String,Inlen);
    }
    
    char *Encryption::HexToString_s(char *Hex, int *OutLen)
    {
        if (Hex == NULL || Hex == nullptr) { return nullptr; }
    
        int HexLen = (int)strlen(Hex);
    
        if (HexLen % 2 == 0)
        {
            int index = 0;
            int len = HexLen / 2;
            char *buf = new char[len + 1];
            memset(buf, 0, len + 1);
            *OutLen = len;
            for (int i = 0; i < len; i++)
            {
                unsigned char var = Encryption::HexToStr(Hex + index);
                buf[i] = var;
                index += 2;
            }
            return buf;
        }
        return NULL;
    }
    
    char *Encryption::HexToString_s(const char *Hex, int *OutLen)
    {
        return Encryption::HexToString_s((char*)Hex, OutLen);
    }

    }

  • 相关阅读:
    Neutron介绍
    docker下jenkins环境搭建
    OpenStack allinone环境搭建
    go中channel的部分理解
    总结一下我的面试经历
    MYSQL基础总结(1)
    struts2中判断数组的长度
    antd组件Checkbox
    React使用antd组件Radio
    React使用Echarts
  • 原文地址:https://www.cnblogs.com/YZFHKMS-X/p/11969660.html
Copyright © 2011-2022 走看看