[Qt-interest] How to get unicode data from socket?
Sean Harmer
sean.harmer at maps-technology.com
Thu Jan 14 12:51:41 CET 2010
On Thursday 14 January 2010 11:41:11 Bradley T. Hughes wrote:
> On 01/14/2010 12:26 PM, Sean Harmer wrote:
> > Hi,
> >
> > On Thursday 14 January 2010 11:20:00 Alex 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?
> >
> > QString has a constructor that takes a QByteArray:
> >
> > http://doc.trolltech.com/4.6/qstring.html#QString-8
>
> But this function is documented to convert using toAscii().
>
> Depending on how the OPs string is encoded, you'll probably want to use
> fromUtf8(), fromUtf16(), or possibly the UTF-32 codec (iirc, there is one
> in Qt, but I'm too lazy to look).
>
True. I forgot the OP had unicode data (long day already). There are both
fromUtf8() and fromUtf16() functions. The fromUtf8() one is even linked from
the above section.
Sean
More information about the Qt-interest-old
mailing list