Solved How to decode ISO-8859-2
-
Hi!
How to decode ISO-8859-2?
This code does not decode.QString AppCore::returnNormalText(QString text) { QString text2; QByteArray byteArray; byteArray = text.toLocal8Bit(); QTextCodec* defaultTextCodec = QTextCodec::codecForName("ISO-8859-2"); QTextDecoder *decoder = new QTextDecoder(defaultTextCodec); text2 = decoder->toUnicode(byteArray); return text2; }
-
Hi, Edited I think because of
byteArray = text.toLocal8Bit();
toLocal8Bit() on Windows is not 100% that is uses "ISO-8859-2" (on my Windows PC it uses "ISO-8859-1")so instead you could try:
QString text2; QTextCodec* defaultTextCodec = QTextCodec::codecForName("ISO-8859-2"); QTextDecoder *decoder = new QTextDecoder(defaultTextCodec); QByteArray byteArray; byteArray = defaultTextCodec->fromUnicode(text); text2 = decoder->toUnicode(byteArray); ...
-
It's too not work.
qDebug() return: "text : "?????????? ??????" text2 : "?????????? ??????""
I get the text from QML, maybe it is incorrectly transmitted?QString text2; QTextCodec* defaultTextCodec = QTextCodec::codecForName("ISO-8859-2"); QTextDecoder *decoder = new QTextDecoder(defaultTextCodec); QByteArray byteArray; byteArray = defaultTextCodec->fromUnicode(text); text2 = decoder->toUnicode(byteArray); qDebug()<<"text : "<<text<<"text2 : "<<text2; return text2;
-
Sure, it could be some bad input from QML, I just tried a test of all the "special" characters in ISO-8859-2 (and then text and text2 are the same):
QString text2; QTextCodec* defaultTextCodec = QTextCodec::codecForName("ISO-8859-2"); QTextDecoder *decoder = new QTextDecoder(defaultTextCodec); text = ""; // reset text with test value for (int i = 161; (i < 255); ++i) text += decoder->toUnicode(QByteArray(1,char(i))); QByteArray byteArray; byteArray = defaultTextCodec->fromUnicode(text); text2 = decoder->toUnicode(byteArray); qDebug()<<"text : "<<text<<"text2 : "<<text2;
-
Just realized, if your QML code has some ISO-8859-2 characters it wants translated into Unicode (QString) than it shouldn't pass them to C++ inside a QString (text) because that implies that the translation to Unicode has already taken place
Perhaps you can use a QByteArray, e.g.QString AppCore::returnNormalText(QByteArray text)
-
The trouble is that QML gets the code from the results of the query in XML. Can this text in QML convert to bitarray and convey?
-
Tried to convert the downloaded text from the file to C++, does not work.
AppCore::AppCore() { //QFile file("E:/QTProject/AnexTour/xmlExample.txt"); QFile file(":/Images/xmlExample.txt"); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug()<<"file open"; testXmlString = returnNormalText(file.readAll()); qDebug()<<testXmlString; } } QString AppCore::returnNormalText(QString text) { QString text2; QTextCodec* defaultTextCodec = QTextCodec::codecForName("ISO-8859-2"); QTextDecoder *decoder = new QTextDecoder(defaultTextCodec); QByteArray byteArray; byteArray = defaultTextCodec->fromUnicode(text); text2 = decoder->toUnicode(byteArray); qDebug()<<"text : "<<text<<"text2 : "<<text2; return text2;
-
@mikeeeeee said in How to decode ISO-8859-2:
Tried to convert the downloaded text from the file to C++, does not work.
Yeah, because you already convert the QByteArray to QString after reading the file (because the parameter
text
inreturnNormalText
isQString
). Remember: QString is Unicode, QByteArray can be every encoding you like.