Qt:16进制字符串数据转整数数值函数

//16进制字符串数据转整数数值
int Setting::qStringHexToInt(QString qStringHex)
{
    
    
    int transValue = 0;
    int base = 1;
    for(int i = 0; i < qStringHex.length(); i++)
    {
    
    
        qDebug("转换的字符是%c",qStringHex.at(i).toLatin1());
        switch (qStringHex.at(i).toLatin1()) {
    
    
            case '0':
                break;
            case '1':
                base = 1;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '2':
                base = 2;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '3':
                base = 3;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '4':
                base = 4;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '5':
                base = 5;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '6':
                base = 6;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '7':
                base = 7;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '8':
                base = 8;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case '9':
                base = 9;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'a':
                base = 10;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'b':
                base = 11;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'c':
                base = 12;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'd':
                base = 13;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'e':
                base = 14;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'f':
                base = 15;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'A':
                base = 10;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'B':
                base = 11;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'C':
                base = 12;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'D':
                base = 13;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'E':
                base = 14;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            case 'F':
                base = 15;
                for(int j = qStringHex.length()-(i+1); j > 0; j--)
                {
    
    
                    base *= 16;
                }
                transValue += base;
                qDebug("此时base=%d, transValue=%d", base,transValue);
                break;
            default:
                break;
        }
    }
    return transValue;
}

猜你喜欢

转载自blog.csdn.net/weixin_44350337/article/details/125877856