Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send special unicode characters via TCP socket?
Forum Update on Monday, May 27th 2025

Send special unicode characters via TCP socket?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 9.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    perfwill
    wrote on 6 Aug 2010, 06:18 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Felix
      wrote on 6 Aug 2010, 07:21 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on 6 Aug 2010, 07:43 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          perfwill
          wrote on 6 Aug 2010, 08:10 last edited by
          #4

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

          THANK YOU VERY MUCH!

          1 Reply Last reply
          0
          • A Offline
            A Offline
            asish
            wrote on 2 Sept 2010, 08:00 last edited by
            #5

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

            Best Regards,

            asish

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Felix
              wrote on 2 Sept 2010, 08:07 last edited by
              #6

              [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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved