QByteArray to QString
-
Hi guys,
I'm in the middle of developing a UDP server and need to convert a QByteArray from an incoming datagram to a QString so that I may have my wicked way with it. However I do not know how to convert from one to the other and there seems to be a million ways on Google all of which are disputed.
Thanks
Andrew -
http://doc.qt.nokia.com/latest/qstring.html#QString-9 just use he constructor of QString or operatr= http://doc.qt.nokia.com/latest/qstring.html#operator-eq-4 etc check the QString and Qbyte array documentations for more info
-
It all depends on the encoding of the text the bytearray contains. If it comes in UTF-8, use QString::fromUtf8(), if it is latin1, use QString::fromLatin1(). Accordingly for ASCII. If you need another codec, use QTextCodec::codecForName and QTextCodec::toUnicode().
-
Did you know that every installation of Qt brings the fine API docs with it? No need to ask Google (although a search for "qstring qbytearray" points you to the solution on the first page of results).
Start Qt Assistant or press F1 in Qt Creator. The docs are even "online":http://doc.trolltech.com/4.7/qstring.html.
-
[quote author="Olorin5800" date="1296512233"]http://doc.qt.nokia.com/latest/qstring.html#QString-9 just use he constructor of QString or operatr= http://doc.qt.nokia.com/latest/qstring.html#operator-eq-4 etc check the QString and Qbyte array documentations for more info[/quote]
I strongly advise against using those implicit conversions when handling protocol parsing. They cause unwanted conversions and blur the distinction between unicode and byte array data coming in other encodings than ASCII. I would even consider to diable them completely by defining QT_NO_CAST_FROM_ASCII, QT_NO_CAST_TO_ASCII and QT_NO_CAST_FROM_BYTEARRAY.
-
Apologies, me trying to overcomplicate things ^^. I have been dealing with unmanaged (not so DXUT) DirectX 10 all day so im paranoid of everything and assume nothing to be as easy as I'd first imagine, lol.
Thanks guys for grounding me a little ^^
Andrew