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

QByteArray to QString

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 10.8k 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.
  • A Offline
    A Offline
    awood969
    wrote on last edited by
    #1

    Hi guys,

    I'm in the middle of developing a UDP server and need to convert a QByteArray from an incoming datagram to a QString so that I may have my wicked way with it. However I do not know how to convert from one to the other and there seems to be a million ways on Google all of which are disputed.

    Thanks
    Andrew

    I'm only asking as I'm stupid :-)

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Olorin5800
      wrote on last edited by
      #2

      http://doc.qt.nokia.com/latest/qstring.html#QString-9 just use he constructor of QString or operatr= http://doc.qt.nokia.com/latest/qstring.html#operator-eq-4 etc check the QString and Qbyte array documentations for more info


      My programs never have bugs, they just develop random features.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Frank
        wrote on last edited by
        #3

        It all depends on the encoding of the text the bytearray contains. If it comes in UTF-8, use QString::fromUtf8(), if it is latin1, use QString::fromLatin1(). Accordingly for ASCII. If you need another codec, use QTextCodec::codecForName and QTextCodec::toUnicode().

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Did you know that every installation of Qt brings the fine API docs with it? No need to ask Google (although a search for "qstring qbytearray" points you to the solution on the first page of results).

          Start Qt Assistant or press F1 in Qt Creator. The docs are even "online":http://doc.trolltech.com/4.7/qstring.html.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Frank
            wrote on last edited by
            #5

            [quote author="Olorin5800" date="1296512233"]http://doc.qt.nokia.com/latest/qstring.html#QString-9 just use he constructor of QString or operatr= http://doc.qt.nokia.com/latest/qstring.html#operator-eq-4 etc check the QString and Qbyte array documentations for more info[/quote]

            I strongly advise against using those implicit conversions when handling protocol parsing. They cause unwanted conversions and blur the distinction between unicode and byte array data coming in other encodings than ASCII. I would even consider to diable them completely by defining QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII and QT_NO_CAST_FROM_BYTEARRAY.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              awood969
              wrote on last edited by
              #6

              Apologies, me trying to overcomplicate things ^^. I have been dealing with unmanaged (not so DXUT) DirectX 10 all day so im paranoid of everything and assume nothing to be as easy as I'd first imagine, lol.

              Thanks guys for grounding me a little ^^

              Andrew

              I'm only asking as I'm stupid :-)

              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