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. how to copy a char array to a QString
Forum Update on Monday, May 27th 2025

how to copy a char array to a QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 7.5k 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.
  • M Offline
    M Offline
    ManiRon
    wrote on 15 Feb 2019, 12:03 last edited by
    #1

    I want to memcpy a char array to a QString . Is there any way i can memcpy it ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Feb 2019, 12:18 last edited by
      #2

      Hi,

      No need for memcpy. Depending on what your char array contains:
      QString::fromLocal8Bit
      QString::fromLatin1
      QString::fromUtf8

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

      M 3 Replies Last reply 15 Feb 2019, 12:23
      3
      • S SGaist
        15 Feb 2019, 12:18

        Hi,

        No need for memcpy. Depending on what your char array contains:
        QString::fromLocal8Bit
        QString::fromLatin1
        QString::fromUtf8

        M Offline
        M Offline
        ManiRon
        wrote on 15 Feb 2019, 12:23 last edited by
        #3

        @SGaist I tried sir but actually i facing some problem in trasmitting data through QTcpSocket

        1 Reply Last reply
        0
        • S SGaist
          15 Feb 2019, 12:18

          Hi,

          No need for memcpy. Depending on what your char array contains:
          QString::fromLocal8Bit
          QString::fromLatin1
          QString::fromUtf8

          M Offline
          M Offline
          ManiRon
          wrote on 15 Feb 2019, 12:26 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S SGaist
            15 Feb 2019, 12:18

            Hi,

            No need for memcpy. Depending on what your char array contains:
            QString::fromLocal8Bit
            QString::fromLatin1
            QString::fromUtf8

            M Offline
            M Offline
            ManiRon
            wrote on 15 Feb 2019, 12:28 last edited by ManiRon
            #5

            @SGaist
            this is my code:

            class TcpClient *client;

            MainWindow.cpp


            onReceivingDataToBeSent(TxCommandPacket * command_packet)
            {
            char cmd_array[300] = {0};
            QString d;

            memcpy(d.data(),command_packet,sizeof(TxCommandPacket));
            qDebug("DATA %s",d.data());
            client->sendCommandPacket(d);
            

            }

            tcpClient.cpp


            sendCommandPacket(QString d)
            {
            QByteArray arrBlock;
            QDataStream out(&arrBlock, QIODevice::WriteOnly);
            out<<quint16(0)<<d;
            out << d;

            //out.device()->seek(0);
            out << quint16(arrBlock.size() - sizeof(quint16));
            
            tcpSocket->write(arrBlock);
            

            }

            Structure:


            struct{
            int Mode;
            int iTestName;
            char arrConfigurationData[500];
            int iSizeOfData;
            int iChecksum;
            }TxCommandPacket;

            J 1 Reply Last reply 15 Feb 2019, 12:55
            0
            • M ManiRon
              15 Feb 2019, 12:28

              @SGaist
              this is my code:

              class TcpClient *client;

              MainWindow.cpp


              onReceivingDataToBeSent(TxCommandPacket * command_packet)
              {
              char cmd_array[300] = {0};
              QString d;

              memcpy(d.data(),command_packet,sizeof(TxCommandPacket));
              qDebug("DATA %s",d.data());
              client->sendCommandPacket(d);
              

              }

              tcpClient.cpp


              sendCommandPacket(QString d)
              {
              QByteArray arrBlock;
              QDataStream out(&arrBlock, QIODevice::WriteOnly);
              out<<quint16(0)<<d;
              out << d;

              //out.device()->seek(0);
              out << quint16(arrBlock.size() - sizeof(quint16));
              
              tcpSocket->write(arrBlock);
              

              }

              Structure:


              struct{
              int Mode;
              int iTestName;
              char arrConfigurationData[500];
              int iSizeOfData;
              int iChecksum;
              }TxCommandPacket;

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Feb 2019, 12:55 last edited by
              #6

              @ManiRon said in how to copy a char array to a QString:

              memcpy(d.data(),command_packet,sizeof(TxCommandPacket));

              This can't work this way. Why do you think you have to use memcpy? Why don't you use one of the methods @SGaist suggested?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • M Offline
                M Offline
                mpergand
                wrote on 15 Feb 2019, 12:57 last edited by
                #7

                @ManiRon said in how to copy a char array to a QString:

                memcpy(d.data(),command_packet,sizeof(TxCommandPacket));

                Actually, d.data() returns a pointer to QChar, not char.
                How do you expect the size of the data buffer is ?

                Why not simply use QByteArray:

                QByteArray dataArray(command_packet, sizeof(TXCommandPacket));

                1 Reply Last reply
                5
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 15 Feb 2019, 13:05 last edited by
                  #8

                  With a bit more context, it's easier to give you better answers.

                  Since you are dealing with QTcpSocket, there's absolutely no need to QString at all. As @mpergand suggests, move your code to QByteArray.

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

                  M 3 Replies Last reply 16 Feb 2019, 03:55
                  3
                  • S SGaist
                    15 Feb 2019, 13:05

                    With a bit more context, it's easier to give you better answers.

                    Since you are dealing with QTcpSocket, there's absolutely no need to QString at all. As @mpergand suggests, move your code to QByteArray.

                    M Offline
                    M Offline
                    ManiRon
                    wrote on 16 Feb 2019, 03:55 last edited by
                    #9

                    @SGaist how can i do that sir?

                    M 1 Reply Last reply 16 Feb 2019, 03:57
                    0
                    • M ManiRon
                      16 Feb 2019, 03:55

                      @SGaist how can i do that sir?

                      M Offline
                      M Offline
                      ManiRon
                      wrote on 16 Feb 2019, 03:57 last edited by
                      #10

                      @ManiRon

                      connect(threadAutoManualTest,&TestThread::data_to_be_sent,this,&MainWindow::onReceivingDataToBeSent); i have used signals and slot . I will get the data in a struct and pass it to the function (sendCommandPacket(QString d)) and from there i will transmit it through socket.

                      1 Reply Last reply
                      0
                      • S SGaist
                        15 Feb 2019, 13:05

                        With a bit more context, it's easier to give you better answers.

                        Since you are dealing with QTcpSocket, there's absolutely no need to QString at all. As @mpergand suggests, move your code to QByteArray.

                        M Offline
                        M Offline
                        ManiRon
                        wrote on 16 Feb 2019, 04:01 last edited by
                        #11

                        @SGaist Now i changed function something like this

                        void TcpClient::sendCommandPacket(TxCommandPacket *d)
                        {
                        QByteArray arrBlock;
                        QDataStream out(&arrBlock, QIODevice::WriteOnly);
                        out<<quint16(0)<<d;
                        out << d;

                        //out.device()->seek(0);
                        out << quint16(arrBlock.size() - sizeof(quint16));
                        
                        tcpSocket->write(arrBlock);
                        

                        }

                        void MainWindow::onReceivingDataToBeSent(TxCommandPacket * command_packet)
                        {
                        char cmd_array[300] = {0};
                        QString d;

                        //memcpy(d.data(),command_packet,sizeof(TxCommandPacket));
                        //qDebug("DATA %s",d.data());
                        
                        //QByteArray dataArray(command_packet, sizeof(TxCommandPacket));
                        
                        client->sendCommandPacket(command_packet);
                        

                        }

                        whether this is correct sir?

                        1 Reply Last reply
                        0
                        • S SGaist
                          15 Feb 2019, 13:05

                          With a bit more context, it's easier to give you better answers.

                          Since you are dealing with QTcpSocket, there's absolutely no need to QString at all. As @mpergand suggests, move your code to QByteArray.

                          M Offline
                          M Offline
                          ManiRon
                          wrote on 16 Feb 2019, 05:42 last edited by
                          #12

                          @SGaist

                          I checked the data available in server side and it is same as the the size what ever i am sending from client. But when i try to print the data its not showing anything.

                          QTcpSocket clientSocket = (QTcpSocket)sender();
                          char f[500];
                          char *g;
                          TxCommandPacket s;
                          QByteArray d;
                          QDataStream in(clientSocket);
                          //in.setVersion(QDataStream::Qt_5_10);
                          for (;;)
                          {
                          qDebug("SERVER DATA %d",clientSocket->bytesAvailable());
                          qDebug("1 OUTSIDE SERVER DATA %d", m_nNextBlockSize);

                              if (!m_nNextBlockSize) {
                                      if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
                                  in >> m_nNextBlockSize;
                                  qDebug("INSIDE SERVER DATA %d", m_nNextBlockSize);
                              }
                              qDebug("2 OUTSIDE SERVER DATA %d", m_nNextBlockSize);
                              if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
                              QString str;
                              in >> str;
                              memcpy(f,str.toLatin1().data(),sizeof(TxCommandPacket));
                              //g = &s;
                              memcpy(&s,f,sizeof(TxCommandPacket));
                              qDebug("STRUCt DATA %d", s.iTestName);
                          
                          
                          
                              qDebug("STR SERVER DATA %s", str.toLatin1().data());
                          
                              emit gotNewMesssage(str);
                          
                              m_nNextBlockSize = 0;
                          
                              if (sendToClient(clientSocket, QString("Reply: received [%1]").arg(str)) == -1)
                              {
                                  qDebug() << "Some error occured";
                              }
                          }
                          
                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 16 Feb 2019, 23:02 last edited by
                            #13

                            From the looks of it, you are over-complicating things especially trying to use char arrays while you already have types that can easily be used with the QDataStream class.

                            Did you check the fortune server and client examples ?

                            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
                            2

                            1/13

                            15 Feb 2019, 12:03

                            • Login

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