Converting qint8 to string
-
hello,
i want to send an qint8 via tcpip, and for that, after the connection is etablished via the socket,
i want to use the
@
socket->write("GUISTATUS: \n");@
directive
and i want to add one qint8 variable, in which 8 independent informations are included, in each bit one.
how can i put it in the string?it didnt work for me with QDataStream, i think because i didnt use a file.
Thanks for hinds and advices.
-
QDataStream should work just fine. You could prepare your data like this:
@
//pseudo-code, brain to terminal
QByteArray data;
QDataStream s(&data);
s << "GUISTATUS\n";
s << myUint8Variable;
socket->write(data);
@ -
hi,
thanks for the fast reply.
i tried
@ QByteArray data;
QDataStream s(&data);s << guistatus; s << "GUISTATUS\n"; socke->write(data);@
and i get an error:
@C:\Dokumente und Einstellungen\loetrobo\Robbie20-build-desktop-Qt_4_8_0_for_Desktop_-_MinGW__Qt_SDK__Debug..\Desktop\Robbie20\robbie20.cpp:234: error: no matching function for call to 'QDataStream::QDataStream(QByteArray*)'@
-
According to the docs for [[doc:QDataStream]], there is a constructor for a QDataStream that takes a QByteArray pointer and a flag to indicate the opening mode, and there is a constructor that takes a QByteArray reference. The documentation explains why you want the first one when writing to the byte array.
That's why I said "pseudo-code, brain to terminal". You're supposed to interpret that as "this is the general idea, lookup the exact function names and signatures yourself in the documentation".
-
This should solve the compiler error:
@
QDataStream s(&data, QIODevice::ReadWrite);
@ -
hi, thanks thats what i tried
thats my code:
@ qint8 integ = 0b00001111;
QByteArray helparray = "GUISTATUS\n";
QByteArray data;
QDataStream s(&data, QIODevice::ReadWrite);
QString helpstring;s << helparray;
s >> helpstring;
qDebug() << "helpstring: " << helpstring;@i expected to get in my qdebug window, but the QString is totally empty.
@helpstring: "" @
where is my mistake?
i cant find it, didn't know this conversation is such a big deal. -
On line 8 of your code above, you start reading at the current position in the stream. That is: after what you just wrote.
Just use the write-only mode instead, and display your data bytearray directly with qDebug.
-
hi,
ah okay. but when i do like this, i get an " QIODevice::read: WriteOnly device" advice.
@ qint8 integ = 0b00001111;
QByteArray helparray = "GUISTATUS";
QByteArray data;
QDataStream s(&data, QIODevice::WriteOnly);
QString helpstring;s << integ <<" test";
qDebug() << "data: " << data;
s >> helpstring;qDebug() << "helpstring: " << helpstring; @
the output in the qdebug is not correct.
data: " -> incomplete
helpstring: "" -> emptyits important its a string i send, because then i can check if there are unwanted signs inside like /n i could remove manually.
i feel like qt put some of those with the << operator, so i keep full control. -
Hmmm.... Perhaps QDataStream isn't for you after all. I am sorry I suggested it. QDataStream does not only stream the raw data, but also some meta data (like the length of a QByteArray you stream in) if needed. You can circumvent that using QDataStream::writeBytes or QDataStream::writeRawBytes, but that defeats the point of using it in the first place, right?
Note that your warning is caused by your line 9 above. If you use a WriteOnly stream, you can't obviouly read from it. Also, streaming into a QString probably doesn't do what you expect it does... Instead, use one of the QString::from<Encoding>() methods to construct a string from a QByteArray.
Anyway, you can probably get rid of QDataStream completely, as it screws up your data in your case if you add a QByteArray. Instead, you could try this:
@
//again, untested code:
QByteArray data;
data += "GUISTATUS";
data.append( reinterpret_cast<char*>(&myUInt8Variable), 1 ); //Warning: no idea if this will work ok
qDebug() << "data:" << data;
@Or, using QDataStream again, but only for the integer:
@
//again, untested code:
QByteArray data;
data += "GUISTATUS";
QDataStream s(&data, QIODevice::WriteOnly);
s << myUInt8Variable;
qDebug() << "data:" << data;
@ -
did you try QString().setNum(myUInt8Variable) to convert ?
-
[quote author="Neutron Stein" date="1342534822"]did you try QString().setNum(myUInt8Variable) to convert ?[/quote]
He was looking for a QByteArray (to send over a TCP socket). Converting to a string does not help for that.
-
isn't the topic?converting from quint8 to string?
-
[quote author="Neutron Stein" date="1342535272"]isn't the topic?converting from quint8 to string?[/quote]
Not really:
[quote author="roboterprogrammer" date="1342524761"]i want to send an qint8 via tcpip, and for that, after the connection is etablished via the socket,
i want to use the
@
socket->write("GUISTATUS: \n");@
directive
and i want to add one qint8 variable, in which 8 independent informations are included, in each bit one.[/quote]You need to read more than just the subject header... And yes, I sometimes fail to do that as well.
-
I think this can help.
@
QByteArray paquet;
QDataStream data(&paquet,QIODevice::WriteOnly);
data << (quint8)0;
data << theStringToSend;
data.device()->seek(0);
data << (quint8)(paquet.size() - sizeof(quint8));
socket->write(paquet);
@ -
hi,
solution 2 works now, so i can send data (changes that operator made) to gui,
but its not nice.if no better solution i will keep it like this.
the thing is:with @s << myUInt8Variable;@
it writes it to the first postiton of data.
so in qdebug instead of GUISTATUSX i get XUISTATUS
how can i put it on the end?
-
wow so many replys i will read them, and try the new stuff out.
thanks
-
sorry I sent the wrong code.but it might be something like that. there i put the packet size at the beginning of the packet to be sent.i think you just need to put there what you want to send.
Sorry for mistakes i'm newbie