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. Connect QString with byte code
Forum Updated to NodeBB v4.3 + New Features

Connect QString with byte code

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 610 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Damian7546
    wrote on 12 Jan 2023, 18:24 last edited by
    #1

    Hi,
    I try send some text to RS232 device. But some letter are coded according with WPC1250 table code. So I try send text:

    QString sAddresHeader = "wroc\xB3aw" ;

    but it doesnt work. Work when I send "wroc" 0xB3 "aw". It possible connect this in one string ?

    C 1 Reply Last reply 12 Jan 2023, 18:39
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 12 Jan 2023, 19:05 last edited by Chris Kawa 1 Dec 2023, 19:06
      #3

      \x escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write "wroc\xB3aw" it will treat \xB3a as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this: "wroc\xB3""aw".

      But that's just first of your multiple problems here.
      "wroc\xB3""aw" is a character array encoded as WPC1250
      QString is an UTF-16 container
      QString operator= does a conversion assuming the source is UTF-8.
      So this line

      QString sAddresHeader = "wroc\xB3""aw" ;
      

      takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.

      If you want to store that string in QString you need to tell Qt what source encoding it is. For example:

      QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
      

      this will do conversion from WPC1250 to UTF-16 and store it in QString.

      But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
      If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), so

      QByteArray sAddresHeader = "wroc\xB3""aw" ;
      

      Btw. Greetings from sunny Wrocław :)

      D 1 Reply Last reply 13 Jan 2023, 18:01
      1
      • D Damian7546
        12 Jan 2023, 18:24

        Hi,
        I try send some text to RS232 device. But some letter are coded according with WPC1250 table code. So I try send text:

        QString sAddresHeader = "wroc\xB3aw" ;

        but it doesnt work. Work when I send "wroc" 0xB3 "aw". It possible connect this in one string ?

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 12 Jan 2023, 18:39 last edited by
        #2

        @Damian7546 said in Connect QString with byte code:

        but it doesnt work

        What does this mean? How do you try to send the QString.
        From my pov you won't send a string but a QByteArray...

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        2
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 12 Jan 2023, 19:05 last edited by Chris Kawa 1 Dec 2023, 19:06
          #3

          \x escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write "wroc\xB3aw" it will treat \xB3a as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this: "wroc\xB3""aw".

          But that's just first of your multiple problems here.
          "wroc\xB3""aw" is a character array encoded as WPC1250
          QString is an UTF-16 container
          QString operator= does a conversion assuming the source is UTF-8.
          So this line

          QString sAddresHeader = "wroc\xB3""aw" ;
          

          takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.

          If you want to store that string in QString you need to tell Qt what source encoding it is. For example:

          QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
          

          this will do conversion from WPC1250 to UTF-16 and store it in QString.

          But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
          If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), so

          QByteArray sAddresHeader = "wroc\xB3""aw" ;
          

          Btw. Greetings from sunny Wrocław :)

          D 1 Reply Last reply 13 Jan 2023, 18:01
          1
          • C Chris Kawa
            12 Jan 2023, 19:05

            \x escape sequence is greedy, meaning that it will take every following character that is a hex number, so if you write "wroc\xB3aw" it will treat \xB3a as a single character. To avoid this you need to break the string after the escape character. You can do it with a little trick like this: "wroc\xB3""aw".

            But that's just first of your multiple problems here.
            "wroc\xB3""aw" is a character array encoded as WPC1250
            QString is an UTF-16 container
            QString operator= does a conversion assuming the source is UTF-8.
            So this line

            QString sAddresHeader = "wroc\xB3""aw" ;
            

            takes a WPC1250 string, treats it as UTF-8 and converts that to UTF-16, which is of course nonsense.

            If you want to store that string in QString you need to tell Qt what source encoding it is. For example:

            QString sAddresHeader = QTextCodec::codecForName("Windows-1250")->toUnicode("wroc\xB3""aw");
            

            this will do conversion from WPC1250 to UTF-16 and store it in QString.

            But that's still not the end of it. As Christian mentioned you usually send bytes, not QStrings. It's important to know what encoding does the receiver expect - UTF or WPC1250. If you send the bytes of QString and receiver expects WPC1250 then you need to convert it back, either before you send it or after you receive it.
            If the receiver expects WPC1250 then it's easier not to use QString as the storage, and simply store the WPC1250 bytes in QByteArray (again, like Christian suggested), so

            QByteArray sAddresHeader = "wroc\xB3""aw" ;
            

            Btw. Greetings from sunny Wrocław :)

            D Offline
            D Offline
            Damian7546
            wrote on 13 Jan 2023, 18:01 last edited by
            #4

            @Chris-Kawa said in Connect QString with byte code:

            QByteArray sAddresHeader = "wroc\xB3""aw" ;

            This is what I need .
            QByteArray sAddresHeader = "Greetings from Rzesz\xF3""w"

            C 1 Reply Last reply 13 Jan 2023, 19:17
            0
            • D Damian7546
              13 Jan 2023, 18:01

              @Chris-Kawa said in Connect QString with byte code:

              QByteArray sAddresHeader = "wroc\xB3""aw" ;

              This is what I need .
              QByteArray sAddresHeader = "Greetings from Rzesz\xF3""w"

              C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 13 Jan 2023, 19:17 last edited by
              #5

              @Damian7546 That string breaking trick is needed only if a hex character follows the escape sequence, so any of A-F letters. w is not one of them, so in case of Rzeszów you can just do "Rzesz\xF3w"

              1 Reply Last reply
              1
              • hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on 13 Jan 2023, 19:34 last edited by
                #6

                Thank you for that string breaking trick Chris!
                It has got me stumped a few times, in HTML it's easy just do a semicolon (Rzeszów) but not so in C++ :-)

                1 Reply Last reply
                0

                1/6

                12 Jan 2023, 18:24

                • Login

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