Normally when I want to convert the text to string I do the following:
QString::fromUtf16((ushort*)buffer);
But is there any way to convert my buffer (data) to unicode?
Normally when I want to convert the text to string I do the following:
QString::fromUtf16((ushort*)buffer);
But is there any way to convert my buffer (data) to unicode?
The data is never in " Unicode
". The data is in UTF-8
, UTF-16
, UTF-32
, UCS-2
, or any binary format that implement Unicode
. Unicode
is a standard that assigns to each possible character a number.
That standard should be implemented by a coding format such as UTF-8
, for example.
Each encoding format transforms binary values Unicode
differently. That is, the binary representation of a string Unicode
any as Vé por allí ☛
, will be different in UTF-8
and UTF-16
.
If the buffer
is filled with values that come from a database or the user's input, you should know in which encoding the database returns said data (if UTF-8
or UTF-16
), or in what encoding the user is giving you data by standard input, for example.
Once you know the format of the data returned, those same data should be saved unchanged within buffer
. If your buffer
contains data in UTF-8
, then you should call fromUtf8()
, and if they are in UTF-16
, then a fromUtf16()
.
In any case, the phrase "convert to Unicode
" does not make sense because Unicode
is a standard, and nothing that can be "saved".
Fundamentally, if fromUtf16()
is giving you erroneous or strange results, it is because the buffer
is not in UTF-16
. That is, you have to trace the origin of the buffer
data to know what encoding they will have (maybe it's not even a Unicode
code, but ISO-8859-15
, which is not compatible at binary level with UTF-8
per example).