-1

I want to make sth. like the following Code... Has someone an idea? Thank you :)

QString x = QString("\ue001");

if(x.startsWith("\ue")) {
    //...
}

2 Answers2

1

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.

deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • Apart from the string construction, QString is encoded in UTF-16. So to check if the first codepoint contained in the string matches the form U+Exxx, it's enough if you check if the first code unit is indeed between 0xE000 and 0xEFFF (since the U+Exxx range is encoded in UTF-16 into its own value)... – peppe Jul 24 '16 at 16:33
  • Thanks for your answers, but i need to convert the value "001" of QString("\ue001") to another QString => QString("001") ... and i want to convert QString("001") to QString("\ue001"). I need this for an interactive IconFont Userinterfacedesigner... The User has to input : "\ue001" and so he can set an IconFont from Google Material in the UI which is written with Qt Quick. Thank you :) – kunst-der-informatik Jul 26 '16 at 18:27
0

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.