检查UTF-8字符串在Qt中是否有效 [英] Check if UTF-8 string is valid in Qt

查看:414
本文介绍了检查UTF-8字符串在Qt中是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Qt中,有没有一种方法可以检查字节数组是否为有效的UTF-8序列?

In Qt, is there a way to check if a byte array is a valid UTF-8 sequence?

QString :: fromUtf8()似乎无声地禁止或替换无效的序列,而不会通知调用者存在任何序列.这来自其文档:

It seems that QString::fromUtf8() silently suppresses or replaces invalid sequences, without notifying the caller that there were any. This is from its documentation:

但是,无效序列可能与 UTF-8,并且如果找到任何此类,则将它们替换为一个或 更多替换字符",或取消显示.

However, invalid sequences are possible with UTF-8 and, if any such are found, they will be replaced with one or more "replacement characters", or suppressed.

推荐答案

尝试使用 QTextCodec :: toUnicode 并传递 ConverterState 实例. ConverterState具有类似invalidChars的成员.虽然它们没有通过doxygen进行记录,但是我认为它们是公共API,如QTextCodec文档中所述.

Try with QTextCodec::toUnicode and passing a ConverterState instance. ConverterState has members like invalidChars. They are not documented via doxygen though, but I assume them to be public API, as they are mentioned in the QTextCodec documentation.

示例代码:

QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
const QString text = codec->toUnicode(byteArray.constData(), byteArray.size(), &state);
if (state.invalidChars > 0) {
    qDebug() << "Not a valid UTF-8 sequence.";
}

这篇关于检查UTF-8字符串在Qt中是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆