当前位置:网站首页>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();
}
边栏推荐
- docker+bridge+redis master-slave+sentry mode
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- drools从下载到postman请求成功
- SVM介绍以及实战
- 八年软件测试工程师带你了解-测试岗进阶之路
- 备份工具pg_dump的使用《postgres》
- 8. Haproxy builds a web cluster
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- 2022软件测试面试题 最新字节跳动50道真题面试题 刷完已拿下15k 附讲解+答疑
- 【21天学习挑战赛】顺序查找
猜你喜欢

技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris

【源码】使用深度学习训练一个游戏

Explain详解与实践

ADC噪声全面分析 -03- 利用噪声分析进行实际设计
![[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this](/img/d0/6ac51d0d51c907ed0e1578e038fffd.jpg)
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this

Oracle与Postgresql在PLSQL内事务回滚的重大差异

See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

系统设计.秒杀系统

转:管理是对可能性的热爱,管理者要有闯进未知的勇气
随机推荐
学会iframe并用其解决跨域问题
Use serve to build a local server
Jenkins export and import Job Pipeline
Structure function exercise
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
mysql索引笔记
打造一份优雅的简历
centos 安装postgresql13 指定版本
SQL query String field less than 10 how to check
docker+bridge+redis master-slave+sentry mode
How to open a CITIC Securities online account?is it safe?
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
Explain详解与实践
RSS订阅微信公众号初探-feed43
劝退背后。
杭电多校-Slipper-(树图转化+虚点建图)
Hangdian Multi-School-Slipper- (tree map conversion + virtual point mapping)
SQL injection in #, - +, - % 20, % 23 is what mean?
2022年软件测试——精选金融银行面试真题
Functions, recursion and simple dom operations