PDA

View Full Version : Read unicode 8 bit and 16 bit data into QByteArray : Qt, C++, Windows XP



ustulation
22nd June 2012, 09:18
hi,
i'm developing a propriety protocol for my company (programming on the server side) and have a very strict deadline. It was decided that we use Qt. It is required to querry the Window Registry and write the value obtained into a prenegotiated socket. I have read the Registry Data into a QString. For certain data I must use 8-bit unicode characters and for some 16-bit unicode characters. I am using QByteArray to store all the data before finally writing to the socket using QTcpSocket::write(). Little Endianness must be followed.
<1> How do i get the data from QString into QByteArray in unicode 8 bit format (specification says character type corresponds to quint8)?
<2> How do i get the data from QString into QByteArray in unicode 16 bit format (specification says character type corresponds to quint16)?
<3> How to maintain Little Endianness on point number 2?

Thank you

yeye_olive
22nd June 2012, 12:14
Unfortunately your specifications are incomplete: "unicode 8 bit format" and "unicode 16 bit format" are not clearly defined names for Unicode encodings; but, from your description, they seem to correspond to UTF-8 and UTF-16LE respectively. Please make sure that it is indeed what you need, and that you must not include any BOM (byte-order mark). Then you can set a QTextCodec up with the chosen encoding to convert between QStrings and QByteArrays.

ChrisW67
23rd June 2012, 04:21
There does not seem to be an elegant way to turn off the UTF-16 byte-order-mark in QTextCodec.



wchar_t testString[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20,
0x928, 0x92E, 0x938, 0x94D, 0x924, 0x947,
0x00}; // some ASCII some Devanagari
QString fakeStringFromRegistry = QString::fromWCharArray(testString);
qDebug() << "Original string" << fakeStringFromRegistry;

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QByteArray encodedString = codec->fromUnicode(fakeStringFromRegistry);
qDebug() << "UTF-8" << encodedString.toHex();

codec = QTextCodec::codecForName("UTF-16LE");
encodedString = codec->fromUnicode(fakeStringFromRegistry);
qDebug() << "UTF-16LE with byte-order-mark " << encodedString.toHex();
qDebug() << "UTF-16LE without byte-order-mark" << encodedString.mid(2).toHex();