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. QByteArray to QString with embedded escape sequence
QtWS25 Last Chance

QByteArray to QString with embedded escape sequence

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 957 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.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    Problem: apostrophe is encoded as 0x26, 0x23, 0x33, 0x39, 0x3B in QByteArray not converting in QString

    I have a QByteArray result extracted from xmp data in an image file:

    result = {
    0x42, 0x75, 0x66, 0x66, 0x6C, 0x65, 0x68, 0x65, 0x61, 0x64, 0x2C, 0x20,
    0x50, 0x69, 0x70, 0x65, 0x72, 0x26, 0x23, 0x33, 0x39, 0x3B, 0x73, 0x20,
    0x4C, 0x61, 0x67, 0x6F, 0x6F, 0x6E
    }

    The following do not convert properly:

    QString value = QTextCodec::codecForMib(106)->toUnicode(result);
    QString value = QString::fromStdString(result.toStdString());
    

    value = Bufflehead, Piper&#39 ;s Lagoon
    but it should be: Bufflehead, Piper's Lagoon

    JonBJ 1 Reply Last reply
    0
    • R Rory_1

      Problem: apostrophe is encoded as 0x26, 0x23, 0x33, 0x39, 0x3B in QByteArray not converting in QString

      I have a QByteArray result extracted from xmp data in an image file:

      result = {
      0x42, 0x75, 0x66, 0x66, 0x6C, 0x65, 0x68, 0x65, 0x61, 0x64, 0x2C, 0x20,
      0x50, 0x69, 0x70, 0x65, 0x72, 0x26, 0x23, 0x33, 0x39, 0x3B, 0x73, 0x20,
      0x4C, 0x61, 0x67, 0x6F, 0x6F, 0x6E
      }

      The following do not convert properly:

      QString value = QTextCodec::codecForMib(106)->toUnicode(result);
      QString value = QString::fromStdString(result.toStdString());
      

      value = Bufflehead, Piper&#39 ;s Lagoon
      but it should be: Bufflehead, Piper's Lagoon

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Rory_1 said in QByteArray to QString with embedded escape sequence:

      0x26, 0x23, 0x33, 0x39, 0x3B

      That is ', as you have shown below. All is correct, nothing is wrong.

      ' is an HTML or XML entity for an apostrophe/single-quote character. No amount of std-stringing or codec-ing is going to convert that. OTOH if you slap that sequence into, say, an HTML page shown by a browser, or presumably a QTextEdit or QLabel, it should show as the apostrophe.

      R 1 Reply Last reply
      4
      • JonBJ JonB

        @Rory_1 said in QByteArray to QString with embedded escape sequence:

        0x26, 0x23, 0x33, 0x39, 0x3B

        That is ', as you have shown below. All is correct, nothing is wrong.

        ' is an HTML or XML entity for an apostrophe/single-quote character. No amount of std-stringing or codec-ing is going to convert that. OTOH if you slap that sequence into, say, an HTML page shown by a browser, or presumably a QTextEdit or QLabel, it should show as the apostrophe.

        R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #3

        @JonB Thanks!! This worked:

        QTextEdit textEdit(value);
        return textEdit.toPlainText();
        
        JonBJ JKSHJ 2 Replies Last reply
        2
        • R Rory_1

          @JonB Thanks!! This worked:

          QTextEdit textEdit(value);
          return textEdit.toPlainText();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Rory_1
          That is a cheeky use of a QTextEdit! :)

          1 Reply Last reply
          0
          • R Rory_1

            @JonB Thanks!! This worked:

            QTextEdit textEdit(value);
            return textEdit.toPlainText();
            
            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @Rory_1 said in QByteArray to QString with embedded escape sequence:

            QTextEdit textEdit(value);
            return textEdit.toPlainText();
            

            That's quite clever!

            If you need to do the conversion repeatedly, I suggest creating the QTextEdit on the heap to avoid repeated construction and destruction.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              No need to use a QWidget - QTextDocument is enough here (Using QTextDocument::setHtml() / toPlainText())

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              JonBJ 1 Reply Last reply
              4
              • Christian EhrlicherC Christian Ehrlicher

                No need to use a QWidget - QTextDocument is enough here (Using QTextDocument::setHtml() / toPlainText())

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Christian-Ehrlicher
                Ah, that is much better :)

                @Rory_1
                Yes, you can convert HTML to plain text via QTextEdit. But if it's a standalone function to do just that, we prefer not to use a widget/UI method, @Christian-Ehrlicher's QTextDocument is preferable.

                R 1 Reply Last reply
                0
                • JonBJ JonB

                  @Christian-Ehrlicher
                  Ah, that is much better :)

                  @Rory_1
                  Yes, you can convert HTML to plain text via QTextEdit. But if it's a standalone function to do just that, we prefer not to use a widget/UI method, @Christian-Ehrlicher's QTextDocument is preferable.

                  R Offline
                  R Offline
                  Rory_1
                  wrote on last edited by
                  #8

                  @JonB
                  @Christian-Ehrlicher

                  Thanks very much! One thing I did not mention is this is in a worker thread pulling metadata out of images, and is not in the main gui thread. While the QTextEdit solution was working in release mode, it would not run in debug mode. The QTextDocument solution solves this issue as well.

                      QTextDocument d;
                      d.setHtml(value));
                      return d.toPlainText();
                  

                  @JKSH
                  Thanks for the tip about using the heap. :-)

                  JKSHJ 1 Reply Last reply
                  0
                  • R Rory_1

                    @JonB
                    @Christian-Ehrlicher

                    Thanks very much! One thing I did not mention is this is in a worker thread pulling metadata out of images, and is not in the main gui thread. While the QTextEdit solution was working in release mode, it would not run in debug mode. The QTextDocument solution solves this issue as well.

                        QTextDocument d;
                        d.setHtml(value));
                        return d.toPlainText();
                    

                    @JKSH
                    Thanks for the tip about using the heap. :-)

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @Rory_1 said in QByteArray to QString with embedded escape sequence:

                    this is in a worker thread.... While the QTextEdit solution was working in release mode, it would not run in debug mode. The QTextDocument solution solves this issue as well.

                    The fact that it worked in Release mode was a fluke. Widgets cannot be used in worker threads, only in the GUI thread.

                    QTextDocument, OTOH, is documented as reentrant so it can be safely used in a worker thread: https://doc.qt.io/qt-5/qtextdocument.html

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    2

                    • Login

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