how to copy a char array to a QString
-
wrote on 15 Feb 2019, 12:03 last edited by
I want to memcpy a char array to a QString . Is there any way i can memcpy it ?
-
Hi,
No need for memcpy. Depending on what your char array contains:
QString::fromLocal8Bit
QString::fromLatin1
QString::fromUtf8 -
Hi,
No need for memcpy. Depending on what your char array contains:
QString::fromLocal8Bit
QString::fromLatin1
QString::fromUtf8 -
Hi,
No need for memcpy. Depending on what your char array contains:
QString::fromLocal8Bit
QString::fromLatin1
QString::fromUtf8wrote on 15 Feb 2019, 12:26 last edited byThis post is deleted! -
Hi,
No need for memcpy. Depending on what your char array contains:
QString::fromLocal8Bit
QString::fromLatin1
QString::fromUtf8wrote on 15 Feb 2019, 12:28 last edited by 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; -
@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;@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?
-
wrote on 15 Feb 2019, 12:57 last edited by
@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));
-
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.
-
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.
-
wrote on 16 Feb 2019, 03:57 last edited by
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.
-
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.
wrote on 16 Feb 2019, 04:01 last edited by@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?
-
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.
wrote on 16 Feb 2019, 05:42 last edited byI 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/13