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. [solved] QString to QByteArray
Forum Updated to NodeBB v4.3 + New Features

[solved] QString to QByteArray

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.6k Views 1 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.
  • D Offline
    D Offline
    deleted28
    wrote on last edited by
    #1

    Hi,

    what is the most efficient (speed) way to convert a QString
    @"AA 0D 00 FF 0A 00 00 00 FF FF FF FF 00 00"@

    (including SPACEs )

    to a QByteArray conatinig the above data as:

    @0xAA, 0x0D, 0x00, .... , 0xFF, 0x00, 0x00 @

    thx

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You'll have to do some benchmarking.

      @
      QRegularExpression rx("([A-F0-9]{2})");
      text.replace(rx , "0x\1," ).chop(1);
      qDebug() << text.toLatin1();
      @

      is a possibility

      [edit: updated code to avoid strange replacement]

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted28
        wrote on last edited by
        #3

        Hello SGaist,

        your suggestion looks faster than mine, but i can not use it as it.
        Got @error: 'QRegular' was not declared in this scope@
        I included
        @#include <QRegularExpression>
        #include <QRegExp>

        ... QRegular[removed] ... // <
        @

        here is my first trial:
        @ int i;
        QByteArray ba_frag;
        for(i=0;i<text1.size();i+=3) {
        ba_frag.append(QByteArray::fromHex((text1.mid(i,2)).toLatin1()));
        }
        @
        above seems to work

        1 Reply Last reply
        0
        • JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          Hi,

          fromHex() already handles spaces. Just call
          @
          QByteArray ba_frag = QByteArray::fromHex(text1.toLatin1());
          @

          [quote author="SGaist" date="1424034271"]
          @
          text.replace(QRegular[removed]"([A-F0-9]{2})"), "0x\1,").chop(1);
          @
          [/quote]Did the forum censor you??

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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            It seems that Expression followed by ( triggers [removed]

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deleted28
              wrote on last edited by
              #6

              hm, ?

              1 Reply Last reply
              0
              • JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                [quote author="wally123" date="1424157850"]hm, ?[/quote]SGaist typed
                @
                QRegularExpressionn("([A-F0-9]{2})")
                // Remove the extra 'n' above
                @

                but it got converted to...
                @
                QRegular[removed]"([A-F0-9]{2})")
                @
                ...by forum security.

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

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deleted28
                  wrote on last edited by
                  #8

                  Ah, ok :)

                  1 Reply Last reply
                  0

                  • Login

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