如何在 C++ 中将 unicode 小数转换为 ascii 小数?

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

如何在纯 C++ 或 QT6 中将“½”之类的内容转换为“1/2”?

我看到一个答案,它对特定 unicode 字符串的字典进行了硬编码,但是如何对每个可能的小数部分进行硬编码,而不是对所有内容进行硬编码?

c++ qt unicode
1个回答
0
投票

好吧我自己做的哈哈

void convertFractions(QString &input) {

    static QRegularExpression fractionRegex(QStringLiteral("[\u00BC-\u00BE\u2150-\u215E]"));

    QRegularExpressionMatchIterator i = fractionRegex.globalMatch(input);

    while (i.hasNext()) {
        QRegularExpressionMatch match = i.next();
        QString fraction = match.captured(0);

        fraction = fraction.normalized(QString::NormalizationForm_KD);

        input.replace(match.capturedStart(), match.capturedLength(), fraction);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.