string - Converting QString to utf16 hex representation for non ascii characters -
i going convert qstring hex representation, works fine, until put special characters '€':
qstring l_str = "how you"; qdebug() << "hex: " << l_str.toutf8().tohex(); qdebug() << "readable:" << l_str.toutf8();
prints out:
hex: "486f772061726520796f753f"
readable: "how you?"
and easy convert hex values ascii, being 2 values (48 = h etc.) hex representation of ascii char enough iterate , convert every 2 chars.
if set l_str = "h€w ar€ you?", , hex € sign in utf8 "e282ac" 6 values, result following:
hex: "48e282ac77206172e282ac20796f753f"
but how can readable string?
it better having conversion results in utf16 string:
hex: "0048006f20ac00200061007220ac00200079006f0075003f"
consider "ho€ ar€ you" string created @ runtime (so no qstringliteral available), , cannot use auto keyword.
you can convert string utf8/16, , convert buffer holds hexadecimal representation. these steps can followed in reverse order go hex utf8/16 string.
the toutf16hex
function prepends byte order mark both little- , big-endian hosts can correctly decode utf16 representation.
// https://github.com/kubao/stackoverflown/tree/master/questions/str-to-utf-38831190 #include <qtcore> qbytearray toutf8hex(const qstring & str) { return str.toutf8().tohex(); } qstring fromutf8hex(const qbytearray & hex) { return qstring::fromutf8(qbytearray::fromhex(hex)); } qbytearray toutf16hex(qstring str) { str.prepend(qchar::byteordermark); // ok use `fromrawdata` since tohex copies it. return qbytearray::fromrawdata( reinterpret_cast<const char*>(str.constdata()), (str.size()+1)*2).tohex(); } qstring fromutf16hex(const qbytearray & hex) { const qbytearray utf16 = qbytearray::fromhex(hex); return qstring::fromutf16(reinterpret_cast<const quint16*>(utf16.data())); } int main() { const qstring str = qstringliteral("h€w ar€ you?"); // utf8 , const qbytearray hex8 = toutf8hex(str); q_assert(fromutf8hex(hex8) == str); // utf16 , const qbytearray hex16 = toutf16hex(str); q_assert(fromutf16hex(hex16) == str); }
Comments
Post a Comment