i'm trying to convert some data get from a txt file coded in utf16 to utf8 by using Qt. Could someone help in this? Thank you in advance.
-
`const std::string utf8there = QString( "text" ).toStdString();` – Dmitry Sazonov May 31 '16 at 15:49
-
`QString` has a [`toUtf8`](http://doc.qt.io/qt-5/qstring.html#toUtf8) function. Please at least do a cursory search of the documentation before asking. – jonspaceharper May 31 '16 at 15:58
2 Answers
QString
provides methods to import from UTF-16 and export to UTF-8, and vice versa of course, and many others. The QByteArray
objects used for input/output of these methods are best handled by using QFile
.
You should be able to find your way from there; if you have other problems with using this facilities you're supposed to provide a Minimal, Complete, and Verifiable example in order to get appropriate help.
-
@MarcoFerrara You're welcome :-) Please don't forget to mark the question as answered when it is solved. – Murphy Jun 08 '16 at 09:42
-
This is not an answer. There are a LOT of topics to cover in uhf8 and utf16. – Michał Leon Aug 13 '23 at 18:34
This is the answer: https://doc.qt.io/qt-6/qstringconverter.html
The QStringConverter class is a base class for the QStringEncoder and QStringDecoder classes that help with converting between different text encodings. QStringDecoder can decode a string from an encoded representation into UTF-16, the format Qt uses internally. QStringEncoder does the opposite operation, encoding UTF-16 encoded data (usually in the form of a QString) to the requested encoding.

- 127,765
- 105
- 273
- 257

- 2,108
- 1
- 15
- 15