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.
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.
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.
It is my bad assumption. Thank you, the problem is solved.
THANK YOU VERY MUCH!
I m facing same problem. plz metion how to solve it.I'v spent already 16 hrs but could not solve.
[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.
have you read the thread completly? there is already a solution