Qt笔记:QString::toLocal8Bit的理解
qt文档说明:
QByteArray QString::toLocal8Bit() const
Returns the local 8-bit representation of the string as a QByteArray. The returned byte array is undefined if the string contains characters not supported by the local 8-bit encoding.
QTextCodec::codecForLocale() is used to perform the conversion from Unicode. If the locale encoding could not be determined, this function does the same as toLatin1().
If this string contains any characters that cannot be encoded in the locale, the returned byte array is undefined. Those characters may be suppressed or replaced by another.
大概意思就是:此函数返回一个本地的8bit(即1个字节)数组,QString内部是unicode编码存储,toLocal8Bit函数将QString内部的unicode编码的字符串转换到QTextCodec::codecForLocale()指定的编码字节流,例如:
- QTextCodec::codecForLocale()是UTF-8,那么返回的字节流就是utf8的字节流,如下图,返回“中文”的utf8字节流: e4 b8 ad e6 96 87
- qt支持的一些编码,具体可自行参看QTextCodec的文档介绍