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

QBytearray to character array conversion

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 3.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.
  • C Offline
    C Offline
    chandradeo
    wrote on last edited by
    #1

    Hi Frnds I am new to Qt and want to know how to convert QBytearray to character array so that i can take individual character and do processing. Below is my code which receives data a 14 Byte data into
    QBytearray. Now i want to extract each character of the data and do processing.
    Plz tell me how to do that. Thank u all in advance.

    @ if(udpSocketRxCDP->hasPendingDatagrams())
    {
    int theSize = udpSocketRxCDP->pendingDatagramSize();
    QByteArray datagram;
    datagram.resize(theSize);
    int dgsize = datagram.size();
    udpSocketRxCDP->readDatagram(datagram.data(), dgsize);

    }@
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can use QString::at(), QByteArray::at() or convert to c array using datagram.constData();

      (Z(:^

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        QByteArray is already suitable to do that. Why do you need the data as a char[]?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dbzhang800
          wrote on last edited by
          #4

          Hi, you already used the conversion in your source code: datagram.data()

          [quote author="chandradeo" date="1375351883"]
          @ if(udpSocketRxCDP->hasPendingDatagrams())
          {
          int theSize = udpSocketRxCDP->pendingDatagramSize();
          QByteArray datagram;
          datagram.resize(theSize);
          int dgsize = datagram.size();
          udpSocketRxCDP->readDatagram(datagram.data(), dgsize);

          }@[/quote]
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            chandradeo
            wrote on last edited by
            #5

            [quote author="Andre" date="1375450751"]QByteArray is already suitable to do that. Why do you need the data as a char[]?[/quote]

            Hi Andre can u please give an example of extracting individual data elements
            from QBytearray. Actuall my data received is like ss;mm;hh and i want to display second min and hour in separate EditText.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              chandradeo
              wrote on last edited by
              #6

              [quote author="1+1=2" date="1375451238"]Hi, you already used the conversion in your source code: datagram.data()

              [quote author="chandradeo" date="1375351883"]
              @ if(udpSocketRxCDP->hasPendingDatagrams())
              {
              int theSize = udpSocketRxCDP->pendingDatagramSize();
              QByteArray datagram;
              datagram.resize(theSize);
              int dgsize = datagram.size();
              udpSocketRxCDP->readDatagram(datagram.data(), dgsize);

              }@[/quote]
              

              [/quote]

              can you please tell me how shall i extract each elements from datagram.data.Actuall my data received is ss;mm;hh and i want to display
              hour min and sec in seperate EditText.

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                [quote author="chandradeo" date="1375451788"]
                [quote author="Andre" date="1375450751"]QByteArray is already suitable to do that. Why do you need the data as a char[]?[/quote]

                Hi Andre can u please give an example of extracting individual data elements
                from QBytearray. Actuall my data received is like ss;mm;hh and i want to display second min and hour in separate EditText.
                [/quote]

                I've already told you: use QByteArray::at().

                But for your usecase, much better is QString::split():
                @
                QString myData(datagram);
                QStringList myParts = myData.split(";");

                // Now myParts.at(0) == "ss"
                // myParts.at(1) == "mm"
                // myPart.at(2) == "hh"
                @

                (Z(:^

                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