PDA

View Full Version : Get QChar code in certain encoding



snorlax212
1st November 2015, 04:49
Hello. For example, i have QChar storing non-latin symbol:


QChar c = 'г';

I can get unicode num of this, but i need code in Windows-1251 encoding (cyrillic), so it should be 227 (decimal). How can i get it?
And also, i want my program to use this encoding by default when converting between nums and chars.

ChrisW67
1st November 2015, 09:13
See QTextCodec:


QChar c(0x0432);
QTextCodec *codec = QTextCodec::codecForName("Windows-1251");
QByteArray encodedChar = codec->fromUnicode(&c, 1);


If you use QTextStream to interact with files encoded in Windows-1251:


textStream->setCodec(QTextCodec::codecForName("Windows-1251"));

These may also be useful:


QTextCodec::setCodecForCStrings(QTextCodec::codecF orName("Windows-1251"));

QTextCodec::setCodecForLocale(QTextCodec::codecFor Name("Windows-1251"));