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. Converting from UTF-16 to UTF-8
Forum Updated to NodeBB v4.3 + New Features

Converting from UTF-16 to UTF-8

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.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.
  • KuvarsK Offline
    KuvarsK Offline
    Kuvars
    wrote on last edited by
    #1

    Hello guys,
    I am receiving a QByteArray in UTF-16. For instance, if I send 'ç' character from my QT application, I receive 0xC3 0xA7, which is UTF-8. However, if I use Hercules application of DW Group, I receive 0x00E7, which is the UTF-16 representation of 'ç'.
    I could not use QString::fromUTF16(), because I could not figure out const char* in the parameter. Moreover, since I do not use QT6, I cannot access QStringConverter class, or new encoding-decoding classes.
    So my Question is, how to display a message in UTF-16 in UTF-8 ? Thank you in advance.

    jsulmJ 1 Reply Last reply
    0
    • KuvarsK Kuvars

      Hello guys,
      I am receiving a QByteArray in UTF-16. For instance, if I send 'ç' character from my QT application, I receive 0xC3 0xA7, which is UTF-8. However, if I use Hercules application of DW Group, I receive 0x00E7, which is the UTF-16 representation of 'ç'.
      I could not use QString::fromUTF16(), because I could not figure out const char* in the parameter. Moreover, since I do not use QT6, I cannot access QStringConverter class, or new encoding-decoding classes.
      So my Question is, how to display a message in UTF-16 in UTF-8 ? Thank you in advance.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kuvars said in Converting from UTF-16 to UTF-8:

      because I could not figure out const char*

      https://doc.qt.io/qt-6/qbytearray.html#constData

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • KuvarsK Offline
        KuvarsK Offline
        Kuvars
        wrote on last edited by Kuvars
        #3

        Thank you, now I can convert a QByteArray to const char*. However, in QString class, fromUtf16 takes a const char16_t* , and it does not accept a const char .
        QString::fromUtf16(const char16_t unicode, qsizetype size = -1)
        Should I change my const char
        to char16_t
        , and how?

        jsulmJ 1 Reply Last reply
        0
        • KuvarsK Kuvars

          Thank you, now I can convert a QByteArray to const char*. However, in QString class, fromUtf16 takes a const char16_t* , and it does not accept a const char .
          QString::fromUtf16(const char16_t unicode, qsizetype size = -1)
          Should I change my const char
          to char16_t
          , and how?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Kuvars said in Converting from UTF-16 to UTF-8:

          Should I change my const char to char16_t , and how?

          Cast the pointer using reinterpret_cast

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Kuvars said in Converting from UTF-16 to UTF-8:

            Should I change my const char to char16_t , and how?

            Cast the pointer using reinterpret_cast

            S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            @jsulm said in Converting from UTF-16 to UTF-8:

            Cast the pointer using reinterpret_cast

            Or even better (if you already use C++20): Use bit_cast instead: https://en.cppreference.com/w/cpp/numeric/bit_cast

            1 Reply Last reply
            0
            • C Offline
              C Offline
              candy76041820
              wrote on last edited by
              #6

              Why not QTextCodec?

              QTextCodec *ptc=QTextCodec::codecForName("UTF-16LE");//Or BE, can't remember
              QString str=ptc->toUnicode(QByteArray("\xE7\x00"));
              
              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