Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if UTF-8 string is valid in Qt

Tags:

c++

utf-8

qt

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

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

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.

like image 965
sashoalm Avatar asked Aug 14 '13 09:08

sashoalm


2 Answers

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.

Sample code:

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.";
}
like image 81
Frank Osterfeld Avatar answered Sep 16 '22 11:09

Frank Osterfeld


The ConverterState way, which has already been reported here by Frank Osterfeld, works even if the text hasn't got a "BOM (Byte Order Mark)" (*).

(*) Unlike QTextCodec::codecForUtfText(), which needs a BOM in the text in order to know that it's in Utf-8.

like image 27
Ganton Avatar answered Sep 17 '22 11:09

Ganton