Send special unicode characters via TCP socket?
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.
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.