[Qt-interest] How to get unicode data from socket?

Nikolay Moskvichev nikolay.moskvichev at gmail.com
Thu Jan 14 14:00:01 CET 2010


Hello, Alex
14.01.2010 16:20 you wrote:
> Hi,
> 
> 
> I have a program written in QT and it receives data via socket connection.
> The data format is:
> +--------------------+-----------------------+
> *| sizeOfData(4bytes) | Data (Unicode string) |*
> +--------------------+-----------------------+
> My questions are:
> 
>    1. After I read the first 4 bytes data, I use memcpy() to get/convert the
>    following data size. Does QT provide a convenient way to achieve this
>    instead of memcpy()?
>    2. I use QTcpSocket.read(sizeOfData), which returns the QByteArray, to
>    read the real data but I can't find a way to convert it to QString. How
>    should I do?
> 
> 

You can use

QString QString::fromUtf8 ( const ushort * unicode, int size = -1 )
or
QString QString::fromUtf16 ( const ushort * unicode, int size = -1 )
depending of what your data really are.
In the last case if data endianness don't match your platform,  your
unicode string should include BOM,


> Thanks.
> Alex
> 




More information about the Qt-interest-old mailing list