问题描述
在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:
推荐答案
尝试使用 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中是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!