简体   繁体   中英

How convert QChar to char code in cp866 table?

How get int code of qchar in this table http://www.ascii-codes.com/cp866.html ?

Here is my code:

int getCp866Code(QChar c) {
    if (!c.isSurrogate()) {
        QString temp = c;
        QTextCodec* cp866 = QTextCodec::codecForName("IBM 866");
        QByteArray byteArray = cp866->fromUnicode(temp);
        return (int) byteArray[0];
    }
    return -1;
}

getCp866Code('ж') // returns -90, not 166

The question is ill posed. QChar is a UTF-16 code unit (which also means it can be part of a surrogate pair). Your best shot would be

  1. Check that it's not part of a surrogate pair ( QChar::isSurrogate )
  2. Build a QString consisting only of that char, then use QTextCodec to encode the string in CP866. Then extract the first byte of it.

Note that it's unspecified what you get if your code point is not encodeable in CP866.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM