当前位置:网站首页>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();
}
边栏推荐
- 结构体函数练习
- PL/SQL Some Advanced Fundamental
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
- 数据治理平台项目总结和分析
- 类如何只能静态分配和只能动态分配
- centos 安装postgresql13 指定版本
- Senior PHP development case (1) : use MYSQL statement across the table query cannot export all records of the solution
- Mockito unit testing
- Introduction to mq application scenarios
猜你喜欢
See how DevExpress enriches chart styles and how it empowers fund companies to innovate their business
高效IO模型
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
数组相关 内容 解析
Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
drools从下载到postman请求成功
一文详解DHCP原理及配置
基于 SSE 实现服务端消息主动推送解决方案
Tensors - Application Cases
外卖店优先级
随机推荐
张量篇-应用案例
2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
文件内容的操作
拿捏JVM性能优化(自己笔记版本)
Postgresql source code (66) insert on conflict grammar introduction and kernel execution process analysis
文件系统的简单操作
7-2 LVS+DR概述与部署
一文详解DHCP原理及配置
if,case,for,while
7.LVS负载均衡群集之原理叙述
JVM的内存模型简介
PL/SQL Some Advanced Fundamental
杭电多校-Slipper-(树图转化+虚点建图)
MRS: Introduction to the use of Alluxio
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
【21 Days Learning Challenge】Direct Insertion Sort
【21天学习挑战赛】顺序查找
7-1 LVS+NAT load balancing cluster, NAT mode deployment
大型连锁百货运维审计用什么软件好?有哪些功能?