Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to decode ISO-8859-2

    General and Desktop
    3
    8
    532
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Mikeeeeee last edited by

      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;
      }
      
      1 Reply Last reply Reply Quote 0
      • hskoglund
        hskoglund last edited by hskoglund

        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);
        ...
        
        1 Reply Last reply Reply Quote 5
        • M
          Mikeeeeee last edited by

          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;
          
          1 Reply Last reply Reply Quote 0
          • hskoglund
            hskoglund last edited by

            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;
            
            1 Reply Last reply Reply Quote 1
            • hskoglund
              hskoglund last edited by

              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)

              1 Reply Last reply Reply Quote 2
              • M
                Mikeeeeee last edited by

                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?

                1 Reply Last reply Reply Quote 0
                • M
                  Mikeeeeee last edited by

                  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;
                  
                  aha_1980 1 Reply Last reply Reply Quote 0
                  • aha_1980
                    aha_1980 Lifetime Qt Champion @Mikeeeeee last edited by

                    @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 in returnNormalText is QString). Remember: QString is Unicode, QByteArray can be every encoding you like.

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 4
                    • First post
                      Last post