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

how to copy a char array to a QString

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 7.8k Views 2 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.
  • SGaistS SGaist

    Hi,

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

    ManiRonM Offline
    ManiRonM Offline
    ManiRon
    wrote on last edited by
    #4
    This post is deleted!
    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

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

      ManiRonM Offline
      ManiRonM Offline
      ManiRon
      wrote on 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;

      jsulmJ 1 Reply Last reply
      0
      • ManiRonM ManiRon

        @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;

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on 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 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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

            ManiRonM 3 Replies Last reply
            3
            • SGaistS SGaist

              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.

              ManiRonM Offline
              ManiRonM Offline
              ManiRon
              wrote on last edited by
              #9

              @SGaist how can i do that sir?

              ManiRonM 1 Reply Last reply
              0
              • ManiRonM ManiRon

                @SGaist how can i do that sir?

                ManiRonM Offline
                ManiRonM Offline
                ManiRon
                wrote on 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
                • SGaistS SGaist

                  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.

                  ManiRonM Offline
                  ManiRonM Offline
                  ManiRon
                  wrote on 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
                  • SGaistS SGaist

                    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.

                    ManiRonM Offline
                    ManiRonM Offline
                    ManiRon
                    wrote on 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
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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