将GBK编码(ANSI的简体中文编码)转换为Unicode编码格式:
QByteArray encodedString = "..."; QTextCodec *codec = QTextCodec::codecForName("GBK"); QString string = codec->toUnicode(encodedString);将Unicode编码转换为GBK编码格式:
QString string = "..."; QTextCodec *codec = QTextCodec::codecForName("GBK"); QByteArray encodedString = codec->fromUnicode(string);
下面给出读取GBK编码的TXT文件的示例代码:
QFile file("FileName"); QTextCodec *codec = QTextCodec::codecForName("GBK"); QString line = codec->toUnicode(file.readLine());