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
Qt 6.11 is out! See what's new in the release blog

QString to QByteArray if QString contains unicode chars

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 2.3k Views 2 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.
  • ocgltdO Offline
    ocgltdO Offline
    ocgltd
    wrote on 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
    • ocgltdO ocgltd

      @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.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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 ocgltdO 2 Replies Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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

        ocgltdO 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          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().

          ocgltdO Offline
          ocgltdO Offline
          ocgltd
          wrote on 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)

          Christian EhrlicherC 1 Reply Last reply
          0
          • ocgltdO ocgltd

            @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)

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            ocgltdO 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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.

              ocgltdO Offline
              ocgltdO Offline
              ocgltd
              wrote on 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 Christian EhrlicherC 2 Replies Last reply
              0
              • ocgltdO ocgltd

                @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 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
                • ocgltdO ocgltd

                  @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.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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 ocgltdO 2 Replies Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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 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
                    • Christian EhrlicherC Christian Ehrlicher

                      @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

                      ocgltdO Offline
                      ocgltdO Offline
                      ocgltd
                      wrote on 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

                      • Login

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