Converting from UTF-16 to UTF-8
-
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. -
@Kuvars said in Converting from UTF-16 to UTF-8:
because I could not figure out const char*
-
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? -
@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
-
@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
-
Why not QTextCodec?
QTextCodec *ptc=QTextCodec::codecForName("UTF-16LE");//Or BE, can't remember QString str=ptc->toUnicode(QByteArray("\xE7\x00"));