Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to decode ISO-8859-2
Forum Update on Monday, May 27th 2025

How to decode ISO-8859-2

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.2k Views
  • 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 Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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
    0
    • hskoglundH Online
      hskoglundH Online
      hskoglund
      wrote on last edited by hskoglund
      #2

      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
      5
      • M Offline
        M Offline
        Mikeeeeee
        wrote on last edited by
        #3

        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
        0
        • hskoglundH Online
          hskoglundH Online
          hskoglund
          wrote on last edited by
          #4

          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
          1
          • hskoglundH Online
            hskoglundH Online
            hskoglund
            wrote on last edited by
            #5

            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
            2
            • M Offline
              M Offline
              Mikeeeeee
              wrote on last edited by
              #6

              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
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on last edited by
                #7

                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_1980A 1 Reply Last reply
                0
                • M Mikeeeeee

                  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_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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
                  4

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved