I want to make sth. like the following Code... Has someone an idea? Thank you :)
QString x = QString("\ue001");
if(x.startsWith("\ue")) {
//...
}
I want to make sth. like the following Code... Has someone an idea? Thank you :)
QString x = QString("\ue001");
if(x.startsWith("\ue")) {
//...
}
To get UTF8, you need to replace
QString x = QString("\ue001");
with
QString x = QString(u8"\ue001");
first.
About the check if the first codepoint calue starts with hex. E: A codepoint with value
\ue???
(with any hex digit for each ?) has following binary representation in UTF8 (with 1 or 0 for each ?):
11101110 10?????? 10??????
Assuming the bytes are valid UTF8, the first 1110 indicates that the next two bytes will start with 10, so there is no need to check for that.
So, for the most simple case, it is enough to check if the first byte (if the string is not empty etc.) is equal to 0b11101110
, that is 0xee
.
The not-so-simple case, UTF8, despite having a fixed byte order, can have a BOM. If the string has at least 4 bytes and the first 4 bytes are
0xef 0xbb 0xbf 0xee
then the check is fulfilled too.
I have a Solution from someday from somewhere in the world wide web:
QString SomeClass::convertStringToUnicode(QString stringCode)
{
int idx = -1;
while( (idx = stringCode.indexOf("\\u")) != -1 ) {
int hex = stringCode.mid(idx + 2, 4).toInt(0, 16);
stringCode.replace(idx, 6, QChar(hex));
}
return stringCode;
}
The String has to be start with a double back-slash.