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
Forum Updated to NodeBB v4.3 + New Features

QByteArray to QString with embedded escape sequence

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 987 Views 2 Watching
  • 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 13 Feb 2021, 15:59 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

    J 1 Reply Last reply 13 Feb 2021, 16:52
    0
    • R Rory_1
      13 Feb 2021, 15:59

      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

      J Offline
      J Offline
      JonB
      wrote on 13 Feb 2021, 16:52 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 13 Feb 2021, 17:48
      4
      • J JonB
        13 Feb 2021, 16:52

        @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 13 Feb 2021, 17:48 last edited by
        #3

        @JonB Thanks!! This worked:

        QTextEdit textEdit(value);
        return textEdit.toPlainText();
        
        J J 2 Replies Last reply 13 Feb 2021, 17:50
        2
        • R Rory_1
          13 Feb 2021, 17:48

          @JonB Thanks!! This worked:

          QTextEdit textEdit(value);
          return textEdit.toPlainText();
          
          J Offline
          J Offline
          JonB
          wrote on 13 Feb 2021, 17:50 last edited by
          #4

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

          1 Reply Last reply
          0
          • R Rory_1
            13 Feb 2021, 17:48

            @JonB Thanks!! This worked:

            QTextEdit textEdit(value);
            return textEdit.toPlainText();
            
            J Offline
            J Offline
            JKSH
            Moderators
            wrote on 14 Feb 2021, 08:48 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
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 14 Feb 2021, 09:06 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

              J 1 Reply Last reply 14 Feb 2021, 10:03
              4
              • C Christian Ehrlicher
                14 Feb 2021, 09:06

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

                J Offline
                J Offline
                JonB
                wrote on 14 Feb 2021, 10:03 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 18 Feb 2021, 13:59
                0
                • J JonB
                  14 Feb 2021, 10:03

                  @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 18 Feb 2021, 13:59 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. :-)

                  J 1 Reply Last reply 18 Feb 2021, 15:15
                  0
                  • R Rory_1
                    18 Feb 2021, 13:59

                    @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. :-)

                    J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 18 Feb 2021, 15:15 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

                    3/9

                    13 Feb 2021, 17:48

                    topic:navigator.unread, 6
                    • Login

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