当前位置:网站首页>QT 如何识别文件的编码格式
QT 如何识别文件的编码格式
2022-08-04 04:24:00 【hellokandy】
在日常开发过程中,经常会遇到读取某个文件的内容,相信大家也会遇到读取出现乱码的问题,如果我们在读取文件之前,先检测文件的编码格式,然后再在读取的时候设置对应的编码,是不是问题就解决了呢?经过笔者认(上)真(网)思(百)索(度)一番,于是有了今天这篇“自动识别文件编码”的文章。
示例代码
#include <QtCore/QCoreApplication>
#include <QTextCodec>
#include <QFile>
#include <QDebug>
enum class EncodingFormat : int
{
ANSI = 0,//GBK
UTF16LE,
UTF16BE,
UTF8,
UTF8BOM,
};
/*! * \brief 检查文件编码 */
EncodingFormat FileCharacterEncoding(const QString& fileName)
{
//假定默认编码utf8
EncodingFormat code = EncodingFormat::UTF8;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly))
{
//读取3字节用于判断
QByteArray buffer = file.read(3);
quint8 sz1st = buffer.at(0);
quint8 sz2nd = buffer.at(1);
quint8 sz3rd = buffer.at(2);
if (sz1st == 0xFF && sz2nd == 0xFE)
{
code = EncodingFormat::UTF16LE;
}
else if (sz1st == 0xFE && sz2nd == 0xFF)
{
code = EncodingFormat::UTF16BE;
}
else if (sz1st == 0xEF && sz2nd == 0xBB && sz3rd == 0xBF)
{
code = EncodingFormat::UTF8BOM;
}
else
{
//尝试用utf8转换,如果无效字符数大于0,则表示是ansi编码
QTextCodec::ConverterState cs;
QTextCodec* tc = QTextCodec::codecForName("utf-8");
tc->toUnicode(buffer.constData(), buffer.size(), &cs);
code = (cs.invalidChars > 0) ? EncodingFormat::ANSI : EncodingFormat::UTF8;
}
file.close();
}
return code;
}
如何使用?
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
EncodingFormat code = FileCharacterEncoding(QStringLiteral("兰亭集序-ANSI.txt"));
qDebug() << "code=" << (int)code;
code = FileCharacterEncoding(QStringLiteral("兰亭集序-UTF-16LE.txt"));
qDebug() << "code=" << (int)code;
code = FileCharacterEncoding(QStringLiteral("兰亭集序-UTF-16BE.txt"));
qDebug() << "code=" << (int)code;
code = FileCharacterEncoding(QStringLiteral("兰亭集序-UTF-8.txt"));
qDebug() << "code=" << (int)code;
code = FileCharacterEncoding(QStringLiteral("兰亭集序-BOM-UTF-8.txt"));
qDebug() << "code=" << (int)code;
return a.exec();
}
边栏推荐
- Explain详解与实践
- mysql索引笔记
- 学会iframe并用其解决跨域问题
- A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
- Tensors - Application Cases
- 【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
- mq应用场景介绍
- 结构体函数练习
- This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
- 移动支付线上线下支付场景
猜你喜欢
随机推荐
企业直播风起:目睹聚焦产品,微赞拥抱生态
The Shell function
centos 安装postgresql13 指定版本
7-1 LVS+NAT 负载均衡群集,NAT模式部署
外卖店优先级
Polygon zkEVM network node
42. 接雨水
[Skill] Using Sentinel to achieve priority processing of requests
帮助企业实现数字化转型成功的八项指导原则
7-3 LVS+Keepalived集群叙述与部署
2022支付宝C2C现金红包PHP源码DEMO/兼容苹果/安卓浏览器和扫码形式
【id类型和NSObject指针 ObjectIve-C中】
if,case,for,while
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
SQL query String field less than 10 how to check
10 Convolutional Neural Networks for Deep Learning 3
mysql index notes
RSS订阅微信公众号初探-feed43
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
PL/SQL Some Advanced Fundamental








![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
