0

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

lukaville
  • 182
  • 3
  • 12

1 Answers1

1

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.

peppe
  • 21,934
  • 4
  • 55
  • 70
  • if I am trying convert cyrillic character to character code, my function (in question description) returns negative values :( – lukaville Nov 06 '13 at 13:04