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. QString to quint8 array
QtWS25 Last Chance

QString to quint8 array

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.8k 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.
  • P Offline
    P Offline
    PabloAG
    wrote on last edited by
    #1

    Hi, I have a QString like this: "254 28 0 1 147 155 12 36 74 0 0 14 23".
    I've managed to convert the string to a quint8 array splitting the string and converting each character individually but this method is quite slow. I'm sure there's a faster and smarter way of doing it but I haven't found it yet. ¿How could I achieve this?

    Thanks in advance!

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

      Hi,

      [quote]I’ve managed to convert the string to a quint8 array splitting the string and converting each character individually[/quote]You don't need to convert each character individually.

      Call QString::split(), then

      Call QString::toUInt() or QString::toUShort() (then cast the result to quint8)

      Documentation: http://qt-project.org/doc/qt-5/qstring.html

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PabloAG
        wrote on last edited by
        #3

        Sorry, I don't understand you. I think that's what I'm doing actually.

        @lineReplay = streamReplayLog.readLine();
        sizRead = lineReplay.count(' ');
        listReplay = lineReplay.split(" ");
        for (int i = 0; i < sizRead; i++)
        {
        buffRead[i] = listReplay[i].toUInt();
        }@

        That works but it seems to me that it is a little slow.

        Thanks!

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

          Yes, that's what I meant.

          How slow is it? How long is lineReplay?

          You can speed it up a bit by removing lineReplay.count(). Just use
          @for (int i = 0; i < listReplay.count(); ++i)@

          By the way, your current code misses the last number. Your loop should check i <= sizRead, not i < sizRead

          Also, how do you set the size of buffRead?

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

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            I've hit this kind of problem a few times when writing parser for "big" files like 3d object files.
            Profiling revealed that the problem is with the split method. If the line has like 1000 entries it creates 1000 instances of very small strings just to be discarded by you a line or two later. This is very memory/cache unfriendly and thus slow.

            The way I managed to optimize this is by not splitting the string at all. Instead you can use a QStringRef, which is just a stack based wrapper object around QString and use the indexOf functions to find next numbers. It's a lot faster because it doesn't involve any memory copies or dynamic allocation.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              PabloAG
              wrote on last edited by
              #6

              Thanks. QStringRef seems to work a little faster.

              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