QT检查QString以查看是否是有效的十六进制值

问题描述 投票:0回答:1

使用qregexp

qt hex qstring
1个回答
12
投票
QRegExp

类创建一个正则表达式,以找到所需的内容。 就您而言,以下类似的事情可能会解决问题:

QRegExp hexMatcher("^[0-9A-F]{6}$", Qt::CaseInsensitive); if (hexMatcher.exactMatch(someString)) { // Found hex string of length 6. }

###更新### QT 5用户应考虑使用

QRegularExpression

而不是
QRegExp

QRegularExpression hexMatcher("^[0-9A-F]{6}$",
                              QRegularExpression::CaseInsensitiveOption);
QRegularExpressionMatch match = hexMatcher.match(someString);
if (match.hasMatch())
{
    // Found hex string of length 6.
}

仅使用QString
检查字符串的长度,然后检查一下您可以成功地将其转换为整数(使用基本16转换):

bool conversionOk = false;
int value = myString.toInt(&conversionOk, 16);
if (conversionOk && myString.length() == 6)
{
   // Found hex string of length 6.
}

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.