PDA

View Full Version : Convert Qbytearray data to ascii



Qg90
19th August 2014, 03:05
Hey i have qbytearray data and i want them to be shown to a Qtextedit widget. Right now i get a line of numbers which i need to convert to ascii.
I had the problem this guy (http://stackoverflow.com/questions/10767760/correct-way-to-losslessly-convert-to-and-from-stdstring-and-qbytearray) describes with the "?". I used his example but i get an error i think it's because of the Qtextedit.

d_stranz
19th August 2014, 05:11
To convert QByteArray to ASCII, you should do this, as explained in the StackOverflow post:


std::string stdString(byteArray.constData(), byteArray.length());

If you need a QString, then you can use QString::fromStdString(). If you see non-ASCII characters, it is probably because your byte array contains unicode characters. In this case, you may have to use std::wstring instead of std::string and QString::fromStdWString().

Of course, if your byte array doesn't contain ASCII or unicode characters but is just a binary array, converting it to a string won't work no matter how you do it.

Qg90
19th August 2014, 13:26
Hey thank you this is how it worked.


std::string message(usb_data.constData(), usb_data.length());
QString qmessage = QString::fromStdString(message);