How convert QChar to char code in cp866 table?

1.5k Views Asked by At

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

1

There are 1 best solutions below

1
On

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.