读取0x11,0x12赋值给char

Valencia ·
更新时间:2024-11-10
· 782 次阅读

#include 
#include
int ATOI(const char* str);
void Trim(std::string& value);
void SplitNumsByStr(const char* s, std::vector& v);
struct TypeFunc
{
    unsigned char typeCode;
    unsigned char functionCode;
};
int main()
{
    TypeFunc typeFunc;
    std::string a="0x12,0x11";
    std::vector v;
    SplitNumsByStr(a.c_str(), v);
    typeFunc.functionCode=v[0];
    typeFunc.typeCode=v[1];
    std::cout<<int(typeFunc.functionCode)<<std::endl;
    std::cout<<int(typeFunc.typeCode)<<std::endl;
    return true;
}
void SplitNumsByStr(const char* s, std::vector& v)
{
    int strLen = strlen(s);
    std::string s2;
    for(int i=0; i<strLen; i++)
    {
        if(s[i] == ',')
        {
            Trim(s2);
            if(s2 != "")
            {
                unsigned char  n = ATOI(s2.c_str());
                v.push_back(n);
                s2 = "";
            }
        }
        else
            s2 += s[i];
    }
    Trim(s2);
    if(s2 != "")
    {
        unsigned char n = ATOI(s2.c_str());
        v.push_back(n);
        s2 = "";
    }
}
void Trim(std::string& value)
{
    int i,j;
    char blank = ' ';
    char tab = '	';
    for(i=0; i=0; j--)
        {
            if(value[j] != blank && value[j] != tab && value[j] != 0x0d && value[j] != 0x0a)
                break;
        }
    }
    if(i == value.size())
        value = "";
    else if(i != 0 || j != value.size() - 1)
        value = value.substr(i, j-i+1);
}
int ATOI(const char* str)
{
    if (strlen(str) <= 1)
    {
        return atoi(str);
    }
    if ((str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) ||
        (str[strlen(str)-1] == 'h' || str[strlen(str)-1] == 'H'))
    {
        int i;
        sscanf(str, "%x", &i);
        return i;
    }
    else
    {
        return atoi(str);
    }
}

作者:WYYwyy1111



x11 赋值 char

需要 登录 后方可回复, 如果你还没有账号请 注册新账号