QT字符编码
-
QTString是Unicode编码
-
windows下local8Bit是GBK编码
-
-
vs中的cl能识别GBK
-
Mingw中g++识别不带BOM的UTF-8
-
LINUX中g++识别带BOM的UTF-8
-
-
编码转换
-
QTextCodec:
-
-
非Unicode编码转换成Unicode,Unicode转换成非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
-
-
-
QByteArray encodeString = "俄语字符编码...";
QTextCodec * codec = QTextCodec::codecForName("KOI8-R");
QString unicodeStr = codec->toUnicode(encodestring); -
QString strDefUnicode = "俄语";
QTextCodec * codec = QTextCodec::codecForName("KOI8-R");
QByteArray encodeString = codec->fromUnicode(strDefUnicode);
-
-
编译器或数据库中字符的编码转换
-
QString GBK2UTF8(const QString &str)
{
QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
return utf8->toUnicode(str.toUtf8());
}
//str: unicode解析字符编码,但是存储的确是utf8
UTF8TOGBK(const QString &str)
{
QTextCodec *gbk = QTextCodec::codecForName("GB18030");
//str除了Unicode,可转任意字符编码,转gbk,是因为QTextCodec gbk 只能从gbk转Unicode
return gbk->toUnicode(str.toLocal8Bit());
}
//str:保存的是GBK编码字符串
std::string GBK2UTF8(std::string &str)
{
QString temp = QString::fromLocal8Bit(str.c_str());
std::string ret = temp.toUtf8().data();
}
std::string UTF82GBK(std::string &str)
{
QString temp = QString::fromUtf8(str.c_str());
std::string ret = temp.toLocal8Bit().data();
return ret;
}
QTextCodec::setCodecForCString(QTextCodec::codecForName("GBK"));
QTextCodec::setCodecForCString(QTextCodec::codecForName("UTF-8"));
单QString 只提供了这几个成员函数,远远满足不了大家的需求,比如,在简体中文Windows下,local8Bit是GBK,可是有一个char串是 BIG5 或 Latin-2怎么办?
那就动用强大的QTextCodec吧,首先QTextCodec肯定知道自己所负责的编码的,然后你把一个char串送给它,它就能正确将其转成Unicode了。
QString QTextCodec::toUnicode ( const char * chars ) const
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QString str = tr("中文");
首先说明下windows中API有两种结尾的一种以A结尾一种以W结尾, A代表ANSI编码,W代表UTF-16编码, 这里承清下unicode不是编码方式, 而是字符集.
ANSI编码对于不同的国家和地区制定了不同的编码方式,GB2312(简体中文),BIG5(繁体中文),JIS(日文),在简体中文系统下,ansi 编码代表 GB2312 编码,在日文操作系统下,ansi 编码代表 JIS 编码。这里以简体中文的GB2312为例, 它是用两个字节对汉字进行编码的。
用ANSI的好处在于节约空间,但是坏处在于各地不兼容
UTF-16呢实际上是用1个或者2个16位来进行编码来统一所有字符
UTF-8呢,是用1到4个的8位字符来表示,它也是可变, 英文用7位的ASCII码就可以表示, 拉丁文用8位的Latin1编码就可以表示(表注音),如果有大量的这样的字符,用ANSI或者UTF-16就浪费了空间,因为用一个字节就可以表示了,因为可变长的UTF-8来了,对拉丁文1个字节,对于复杂的用2个甚至4个字节来表示,它有具体的机制来区分到底几个字节表示一个字符。这个机制我就不讲,细节问题。
好了, 关于字符集以及其编码我们先介绍的到这里, 感兴趣的可以参考下面的资料
http://blog.sina.com.cn/s/blog_a547eff001012igt.html http://blog.csdn.net/lvxiangan/article/details/8151670
接着我讲下QTCreator里字符编码的运用,首先要在QTCreator里面调用WINDOWS的API
在.pro文件中加入
LIBS += -luser32 # for using Windows API
QTCreator默认采用unicode字符集, 如果想去掉在.pro文件中加入
DEFINES -= UNICODE
好了,下面来看我们程序
在QtCreator的Editor里面我们可以看到Default encoding 是UTF-8, 好下面我们开始实验
我们先看多字符集也就是加入了 “DEFINES -= UNICODE” 的情况 QByteArray encodedString = "印象笔记"; qDebug() << encodedString;//输出"\xE5\x8D\xB0\xE8\xB1\xA1\xE7\xAC\x94\xE8\xAE\xB0", UTF-8编码
QString string("印象笔记");//UTF-16编码
qDebug() << string;//UTF-16解码成功,输出"印象笔记",
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString string1 = codec->toUnicode(encodedString);//从UTF-8到UTF-16
qDebug() << string1;//UTF-16解码成功,输出"印象笔记"
qDebug() << string1.toUtf8() ;//输出"\xE5\x8D\xB0\xE8\xB1\xA1\xE7\xAC\x94\xE8\xAE\xB0", UTF-8编码
qDebug() << string1.toLocal8Bit() ;//输出"\xD3\xA1\xCF\xF3\xB1\xCA\xBC\xC7", ANSI编码(GB2312)
qDebug() << string1.toStdString().c_str() ;//UTF-8解码成功,输出"印象笔记"
qDebug() << string1.toLatin1() ;//"????"
m_hGame = FindWindow(NULL, string1.toLocal8Bit());//只有这种编码成功,其余都失败(这里API函数为A版本)
qDebug() << m_hGame;
先看下QT中关于QString的官方文档
The QString class provides a Unicode character string.
QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)
这里QString的编码方式理解为UTF-16放在内存中
结论就是A类API函数需要ANSI编码
下面去掉“DEFINES -= UNICODE” 再来看看
QByteArray encodedString = "印象笔记";
qDebug() << encodedString;//输出"\xE5\x8D\xB0\xE8\xB1\xA1\xE7\xAC\x94\xE8\xAE\xB0", UTF-8编码
QString string("印象笔记");//UTF-16编码
qDebug() << string;//UTF-16解码成功,输出"印象笔记",
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString string1 = codec->toUnicode(encodedString);//从UTF-8到UTF-16
qDebug() << string1;//UTF-16解码成功,输出"印象笔记"
qDebug() << string1.toUtf8() ;//输出"\xE5\x8D\xB0\xE8\xB1\xA1\xE7\xAC\x94\xE8\xAE\xB0", UTF-8编码
qDebug() << string1.toLocal8Bit() ;//输出"\xD3\xA1\xCF\xF3\xB1\xCA\xBC\xC7", ANSI编码(GB2312)
qDebug() << string1.toStdString().c_str() ;//UTF-8解码成功,输出"印象笔记",std::string QString::toStdString() const Returns a std::string object with the data contained in this QString. The Unicode data is converted into 8-bit characters using the toUtf8() function.
/* inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }*/
qDebug() << string1.toStdWString().c_str() ;//0x139470, 返回wchar_t*, typedef unsigned short wchar_t;按unsigned short来解释地址指向的值
/*
The std::wstring is encoded in utf16 on platforms where wchar_t is 2 bytes wide
(e.g. windows) and in ucs4 on platforms where wchar_t is 4 bytes wide (most Unix systems).
*/
qDebug() << string1.toLatin1() ;//"????"
m_hGame = FindWindow(NULL, string1.toStdWString().c_str());//只有这种编码成功,其余都失败(这里API函数为W版本)
qDebug() << m_hGame;
WCHAR (or wchar_t on Visual C++ compiler) is used for Unicode UTF-16 strings on windows. on Linux and Mac it's UTF-32 CHAR (or char) can be used for several other string formats: ANSI, MBCS, UTF-8.(关于它们定义 见http://www.firstobject.com/dn_markansifile.htm, 我觉得ANSI和UTF-16对应和MBCS应该和UNICODE对应)
(http://stackoverflow.com/questions/23136837/in-c-when-to-use-wchar-and-when-to-use-char)
结论就是W类API函数需要UTF-16编码
大概总结下,
Qstring里面用UTF-16,转成std::string时用的UTF-8编码,转成std::wstring依靠平台,windows上是UTF-16 当用qdebug输出wchar*时,由于wchar定义为unsigned short,所以其指针指向的单元被解释为unsigned short.
By default, Visual Studio uses the current code page as the source character set used to interpret your source code for output. When a file is read in, Visual Studio interprets it according to the current code page unless the file code page was set, or unless a byte-order mark (BOM) or UTF-16 characters are detected at the beginning of the file. You can't set UTF-8 as the current code page in some versions of Windows. When the automatic detection finds source files encoded as UTF-8 without a BOM in those versions, Visual Studio assumes they're encoded by using the current code page.