August 6, 2010

perfwill perfwill
Lab Rat
2 posts

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.

5 replies

August 6, 2010

Felix Felix
Lab Rat
92 posts

have u tried QString.toUtf8().data() ? Its only a poosibility that might work, i havent tested it.

  1. Returns a UTF-8 representation of the string as a QByteArray.
  2. UTF-8 is a Unicode codec and can represent all characters in a Unicode string like QString
is from the Assistant.

August 6, 2010

Tobias Hunger Tobias Hunger
Area 51 Engineer
3383 posts

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.

August 6, 2010

perfwill perfwill
Lab Rat
2 posts

It is my bad assumption. Thank you, the problem is solved.

THANK YOU VERY MUCH!

September 2, 2010

asish asish
Lab Rat
1 posts

I m facing same problem. plz metion how to solve it.I’v spent already 16 hrs but could not solve.

Best Regards,

asish

September 2, 2010

Felix Felix
Lab Rat
92 posts

asish wrote:
I m facing same problem. plz metion how to solve it.I'v spent already 16 hrs but could not solve.

Best Regards,

asish

have you read the thread completly? there is already a solution

 
  ‹‹ Multiple "valueChanged"-events in QSlider      How to run "Network chat client example" in QT demos ? ››

You must log in to post a reply. Not a member yet? Register here!