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. QString to QByteArray if QString contains unicode chars
QtWS25 Last Chance

QString to QByteArray if QString contains unicode chars

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.4k 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.
  • O Offline
    O Offline
    ocgltd
    wrote on 14 Aug 2022, 18:01 last edited by ocgltd
    #1

    I have a QString which contains some unicode chars, and I want to convert the QString to a QByteArray. (Which is later converted back to a QString)

    If I use toLatin, toUtf8 or toLocal8bit the unicode characters are lost upon conversion.

    How to I convert these unicode character filled QString to a QByteArray?

    1 Reply Last reply
    0
    • O ocgltd
      14 Aug 2022, 19:21

      @Christian-Ehrlicher I'm not sure how to pick encoding for my QString (I could not find it in docs) if that's what you mean. But once that is done, it looks like all of the methods for QString to convert to bytes create a single 8-bit char per character (toUtf-8, to Latin, to local 8 bit)

      How would I get the multibyte QChar to become multiple bytes in the QByteArray? From the docs it sounds like to Utf 8 might create multiple bytes in the QByteArray, but upon testing it did not.

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Aug 2022, 20:00 last edited by
      #7

      @ocgltd said in QString to QByteArray if QString contains unicode chars:

      How would I get the multibyte QChar to become multiple bytes in the QByteArray?

      According to https://unicodeplus.com/U+4FF0 is encoded as 0xE4 0xBF 0xB0 when converted to utf-8

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

      M O 2 Replies Last reply 14 Aug 2022, 20:08
      0
      • C Online
        C Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 14 Aug 2022, 18:04 last edited by
        #2

        When you know which encoding the unicode chars have you can convert them to a proper QString with QTextDecoder. If your QByteArray is utf-8 encoded then use QString::fromUtf8() / toUtf8().

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

        O 1 Reply Last reply 14 Aug 2022, 18:37
        1
        • C Christian Ehrlicher
          14 Aug 2022, 18:04

          When you know which encoding the unicode chars have you can convert them to a proper QString with QTextDecoder. If your QByteArray is utf-8 encoded then use QString::fromUtf8() / toUtf8().

          O Offline
          O Offline
          ocgltd
          wrote on 14 Aug 2022, 18:37 last edited by ocgltd
          #3

          @Christian-Ehrlicher I don't think I know enough to understand the details of your response. For example, I might say:

          QString s = "Hello there";
          s.append(QChar(0x4FF0));
          QByteArray a = s.toLatin();

          But I want the unicode bytes for my unicode char "俰" to appear in the byte array. I haven't picked any encoding (at least not knowingly)

          C 1 Reply Last reply 14 Aug 2022, 18:51
          0
          • O ocgltd
            14 Aug 2022, 18:37

            @Christian-Ehrlicher I don't think I know enough to understand the details of your response. For example, I might say:

            QString s = "Hello there";
            s.append(QChar(0x4FF0));
            QByteArray a = s.toLatin();

            But I want the unicode bytes for my unicode char "俰" to appear in the byte array. I haven't picked any encoding (at least not knowingly)

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 14 Aug 2022, 18:51 last edited by
            #4

            @ocgltd said in QString to QByteArray if QString contains unicode chars:

            But I want the unicode bytes for my unicode char "俰" to appear in the byte array.

            Then you have to use an encoding which supports this unciode char, Latin1 is not one of them but UTF-8.

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

            O 1 Reply Last reply 14 Aug 2022, 19:21
            0
            • C Christian Ehrlicher
              14 Aug 2022, 18:51

              @ocgltd said in QString to QByteArray if QString contains unicode chars:

              But I want the unicode bytes for my unicode char "俰" to appear in the byte array.

              Then you have to use an encoding which supports this unciode char, Latin1 is not one of them but UTF-8.

              O Offline
              O Offline
              ocgltd
              wrote on 14 Aug 2022, 19:21 last edited by ocgltd
              #5

              @Christian-Ehrlicher I'm not sure how to pick encoding for my QString (I could not find it in docs) if that's what you mean. But once that is done, it looks like all of the methods for QString to convert to bytes create a single 8-bit char per character (toUtf-8, to Latin, to local 8 bit)

              How would I get the multibyte QChar to become multiple bytes in the QByteArray? From the docs it sounds like to Utf 8 might create multiple bytes in the QByteArray, but upon testing it did not.

              M C 2 Replies Last reply 14 Aug 2022, 19:46
              0
              • O ocgltd
                14 Aug 2022, 19:21

                @Christian-Ehrlicher I'm not sure how to pick encoding for my QString (I could not find it in docs) if that's what you mean. But once that is done, it looks like all of the methods for QString to convert to bytes create a single 8-bit char per character (toUtf-8, to Latin, to local 8 bit)

                How would I get the multibyte QChar to become multiple bytes in the QByteArray? From the docs it sounds like to Utf 8 might create multiple bytes in the QByteArray, but upon testing it did not.

                M Offline
                M Offline
                mpergand
                wrote on 14 Aug 2022, 19:46 last edited by
                #6

                @ocgltd said in QString to QByteArray if QString contains unicode chars:

                From the docs it sounds like to Utf 8 might create multiple bytes in the QByteArray, but upon testing it did not.

                Why not ?

                QString s = "Hello there";
                s.append(QChar(0x4FF0));
                
                QByteArray a=s.toUtf8();
                s=a;  // back to QString
                
                qDebug()<<s;  // print "Hello there俰"
                
                1 Reply Last reply
                0
                • O ocgltd
                  14 Aug 2022, 19:21

                  @Christian-Ehrlicher I'm not sure how to pick encoding for my QString (I could not find it in docs) if that's what you mean. But once that is done, it looks like all of the methods for QString to convert to bytes create a single 8-bit char per character (toUtf-8, to Latin, to local 8 bit)

                  How would I get the multibyte QChar to become multiple bytes in the QByteArray? From the docs it sounds like to Utf 8 might create multiple bytes in the QByteArray, but upon testing it did not.

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 14 Aug 2022, 20:00 last edited by
                  #7

                  @ocgltd said in QString to QByteArray if QString contains unicode chars:

                  How would I get the multibyte QChar to become multiple bytes in the QByteArray?

                  According to https://unicodeplus.com/U+4FF0 is encoded as 0xE4 0xBF 0xB0 when converted to utf-8

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

                  M O 2 Replies Last reply 14 Aug 2022, 20:08
                  0
                  • C Christian Ehrlicher
                    14 Aug 2022, 20:00

                    @ocgltd said in QString to QByteArray if QString contains unicode chars:

                    How would I get the multibyte QChar to become multiple bytes in the QByteArray?

                    According to https://unicodeplus.com/U+4FF0 is encoded as 0xE4 0xBF 0xB0 when converted to utf-8

                    M Offline
                    M Offline
                    mpergand
                    wrote on 14 Aug 2022, 20:08 last edited by
                    #8

                    @Christian-Ehrlicher said in QString to QByteArray if QString contains unicode chars:

                    According to https://unicodeplus.com/U+4FF0 is encoded as 0xE4 0xBF 0xB0 when converted to utf-8

                    What's right.
                    "Hello there\xE4\xBF\xB0"

                    1 Reply Last reply
                    0
                    • C Christian Ehrlicher
                      14 Aug 2022, 20:00

                      @ocgltd said in QString to QByteArray if QString contains unicode chars:

                      How would I get the multibyte QChar to become multiple bytes in the QByteArray?

                      According to https://unicodeplus.com/U+4FF0 is encoded as 0xE4 0xBF 0xB0 when converted to utf-8

                      O Offline
                      O Offline
                      ocgltd
                      wrote on 14 Aug 2022, 21:36 last edited by
                      #9

                      @Christian-Ehrlicher That is what I wanted, so the reason it doesn't work must be something in my code. I'll recheck. Thanks

                      1 Reply Last reply
                      0

                      9/9

                      14 Aug 2022, 21:36

                      • Login

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