c++ - Qt Convert UTF16 Hex string to QString -


i want convert string in hex , utf16 codec qstring example achieve code:

qstring str1 = qstring::fromwchararray(l"\x0633\x0644\x0627\x0645"); // want 

but when try following code every thing went wrong correct way doing this.

qbytearray hex =  qbytearray::fromhex("0633064406270645"); // wrong text in str2 should equal str1 qstring str2 = qstring::fromutf16((char16_t*)hex.data());  

what wrong code?

qbytearray hex =  qbytearray::fromhex("0633064406270645"); // wrong text in str2 should equal str1 qstring str2 = qstring::fromutf16((char16_t*)hex.data()); 

endian is! equivalent of l"\x3306\x4406\x2706\x4506" , not of l"\x0633\x0644\x0627\x0645".

so overcome problem can add bom string

qbytearray hex =  qbytearray::fromhex("feff0633064406270645"); qstring str2 = qstring::fromutf16((char16_t*)hex.data()); 

didn't test should resolve problem.

alternative solution flip order of bytes in sigle utf-16 character (to change little endian).

qbytearray hex =  qbytearray::fromhex("3306440627064506"); qstring str2 = qstring::fromutf16((char16_t*)hex.data()); 

Comments