包含头文件:
#include <QTextCodec>
栗子,UTF-8 转 GB2312
/* UTF-8 转 GB2312 */
QString str = "这是一段文字。";
QTextCodec *utf8Codec = QTextCodec::codecForName("UTF-8");
QTextCodec *gb2312Codec = QTextCodec::codecForName("GB2312");
QString unicode = utf8Codec ->toUnicode(str.toLocal8Bit().data())));
QByteArray gb2312Bytes = gb2312Codec ->fromUnicode(unicode);
其他编码转换也大致这样,支持的编码有:
Big5
Big5-HKSCS
CP949
EUC-JP
EUC-KR
GB18030
HP-ROMAN8
IBM 850
IBM 866
IBM 874
ISO 2022-JP
ISO 8859-1 to 10
ISO 8859-13 to 16
Iscii-Bng, Dev, Gjr, Knd, Mlm, Ori, Pnj, Tlg, and Tml
KOI8-R
KOI8-U
Macintosh
Shift-JIS
TIS-620
TSCII
UTF-8
UTF-16
UTF-16BE
UTF-16LE
UTF-32
UTF-32BE
UTF-32LE
Windows-1250 to 1258
版权声明:本文为lk_luck原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。