Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to copy a char array to a QString



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


  • Lifetime Qt Champion

    Hi,

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



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



  • This post is deleted!


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


  • Qt Champions 2019

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



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


  • Lifetime Qt Champion

    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.



  • @SGaist how can i do that sir?



  • @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.



  • @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?



  • @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";
        }
    }

  • Lifetime Qt Champion

    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 ?


Log in to reply