Send special unicode characters via TCP socket?
-
wrote on 6 Aug 2010, 06:18 last edited by
I am writing a network chat program and I am having a problem. Qt's functions for writing to socket only accept parameter of type QByteArray and char*. Therefore, special unicode characters such as characters in Chinese, Japanese... cannot be written to the socket.
How can I solve the problem?
Thanks in advance.
-
wrote on 6 Aug 2010, 07:21 last edited by
have u tried QString.toUtf8().data() ? Its only a poosibility that might work, i havent tested it.
@Returns a UTF-8 representation of the string as a QByteArray.
UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString@ is from the Assistant. -
wrote on 6 Aug 2010, 07:43 last edited by
What makes you assume that you can not send unicode characters through sockets? Both QByteArray and char* can be used to send any bitpattern, so it is of course possible to send the bitpatterns that represent unicode characters.
Since there are several popular unicode encodings, you do need to make sure both sides of the sockets agree on the encoding of the characters. One approach was already mentioned by Felix.
If you want to avoid transcoding to/from utf8 (which can be quite costly for non Latin1 characters) you might also try to use QString::data(). That method returns a pointer to QChar which can be reinterpreted as a char * for the purpose of sending it through the socket. That way you would have a utf-16 encoded string.
You might want to prepend a BOM (byte order mark) when using utf-16: This is the preferred way to discover the endianness of the machine sending the string.
-
wrote on 6 Aug 2010, 08:10 last edited by
It is my bad assumption. Thank you, the problem is solved.
THANK YOU VERY MUCH!
-
wrote on 2 Sept 2010, 08:00 last edited by
I m facing same problem. plz metion how to solve it.I'v spent already 16 hrs but could not solve.
Best Regards,
asish
-
wrote on 2 Sept 2010, 08:07 last edited by
[quote author="asish" date="1283414445"]I m facing same problem. plz metion how to solve it.I'v spent already 16 hrs but could not solve.
Best Regards,
asish[/quote]
have you read the thread completly? there is already a solution