QT small case: hexadecimal conversion and digital extraction

1. Given a decimal number, convert it to a two-byte hexadecimal number.

QString get_two_hex(int param)
{
    QString ret = "";
    if(QString::number(param,16).right(4).length() != 2)
    {
        ret = "0" + QString::number(param,16).right(4);
    }else{
        ret = QString::number(param,16).right(4);
    }
    return ret.toUpper();
}
void arr_test()
{
    int arr[] = {43, 12, 51, 176, 24};
    int len = sizeof (arr)/sizeof(arr[0]);
    for(int i=0; i < len; i++)
    {
        qDebug() << get_two_hex(arr[i]);
    }
}

The execution results are as follows:

 

2. Given a string containing multiple floating-point numbers, extract all floating-point numbers from it.

void MainWindow::digital_extraction()
{
    QString data = "apple 10.99 abc 45 12.31 next 37.21 ";

    QRegExp rx("-?(([1-9]\\d*\\.\\d*)|(0\\.\\d*[1-9]\\d*)|([1-9]\\d*))");
    int p = 0;
    qDebug() << "begin";
    QStringList data_list;
    while ((p = rx.indexIn(data, p)) != -1)
    {
        data_list.append(rx.cap(1));
        p += rx.matchedLength(); // 上一个匹配的字符串的长度

    }
    qDebug() << data_list;
    qDebug() << "end";
}

The execution results are as follows:

Guess you like

Origin blog.csdn.net/weixin_47382783/article/details/115051801