PDA

View Full Version : TCP Client issue



LDS
5th May 2010, 16:18
Hello,
I'd like to convert a char array to a string so I can show it in a status label or a text edit box:
Example:


char temp[4] = {0x01, 0x03, 0x03, 0x04};
....
statusLabel->setText(QString::fromAscii(temp, 4));

The status label should show ideally 0x01 0x02 0x03 0x04.

I'm trying to do this in the fortune client example from Qt Creator.



void Client::readFortune()
{
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_3);

char temp[30];
int flag = 0;
char temp2[4] = {0x01, 0x02, 0x03, 0x04};

if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;

in >> blockSize;
}

flag = tcpSocket->readLine(temp, 31);// i'm trying to read the receive buffer here and show it below
if(flag |= -1){
messageLineEdit->setText(QString::number(blockSize)); // I can see the block size, 1537
statusLabel->setText(QString::fromAscii(temp2, 4));
}

if (tcpSocket->bytesAvailable() < blockSize)
return;

QString nextFortune;
in >> nextFortune;

if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}

currentFortune = nextFortune;

statusLabel->setText(currentFortune);

getFortuneButton->setEnabled(true);
}

wysota
5th May 2010, 23:46
Hello,
I'd like to convert a char array to a string so I can show it in a status label or a text edit box:
Example:


char temp[4] = {0x01, 0x03, 0x03, 0x04};
....
statusLabel->setText(QString::fromAscii(temp, 4));

The status label should show ideally 0x01 0x02 0x03 0x04.
0x01-0x04 are unprintable characters. What you want is probably this:

char temp[4] = {0x01, 0x03, 0x03, 0x04};
QStringList hex;
for(int i=0;i<4;i++) {
hex << QString("0x%1").arg((int)temp[i], 2, 16, QLatin1Char('0'));
}
statusLabel->setText(hex.join(" "));

LDS
6th May 2010, 10:17
Thank you for your quick answer.
I know I couldn't possibly print it directly, I already developed de server that sends the data to the client and I had to do the same thing but in C.
Anyway, it worked perfectly!


Thank you for your help,
LDS

Thread solved.