c++分辨读取的文件编码格式是utf-8还是GB2312

直接上代码,有一部分是GPT直接生成的:

#include <QCoreApplication>
#include <QFile>
#include <QTextCodec>
#include <QDebug>

// 判断是否为UTF-8编码
bool isUtf8(const QByteArray &data) {
    int i = 0;
    while (i < data.size()) {
        if ((data[i] & 0x80) == 0) { // 0xxxxxxx
            i++;
            continue;
        }
        if ((data[i] & 0xE0) == 0xC0) { // 110xxxxx 10xxxxxx
            if (i + 1 >= data.size() || (data[i + 1] & 0xC0) != 0x80)
                return false;
            i += 2;
            continue;
        }
        if ((data[i] & 0xF0) == 0xE0) { // 1110xxxx 10xxxxxx 10xxxxxx
            if (i + 2 >= data.size() || (data[i + 1] & 0xC0) != 0x80 || (data[i + 2] & 0xC0) != 0x80)
                return false;
            i += 3;
            continue;
        }
        if ((data[i] & 0xF8) == 0xF0) { // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            if (i + 3 >= data.size() || (data[i + 1] & 0xC0) != 0x80 || (data[i + 2] & 0xC0) != 0x80 || (data[i + 3] & 0xC0) != 0x80)
                return false;
            i += 4;
            continue;
        }
        return false;
    }
    return true;
}

// 判断是否为GB2312编码
bool isGb2312(const QByteArray &data) {
    QTextCodec *codec = QTextCodec::codecForName("GB2312");
    if (!codec)
        return false;

    QString decodedString = codec->toUnicode(data);
    QByteArray encodedData = codec->fromUnicode(decodedString);

    return data == encodedData;
}

// 检测文件编码
QString detectEncoding(const QString &filePath) {
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning() << "Failed to open file:" << filePath;
        return "Unknown";
    }

    QByteArray data = file.readAll();

    if (isUtf8(data)) {
        return "UTF-8";
    } else if (isGb2312(data)) {
        return "GB2312";
    } else {
        return "Unknown";
    }
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    QString filePath = "path/to/your/file.txt";
    QString encoding = detectEncoding(filePath);
    qDebug() << "File encoding:" << encoding;

    return a.exec();
}

代码解释

  1. isUtf8函数:通过检查字节模式来判断数据是否符合UTF-8编码的格式。
  2. isGb2312函数:使用Qt的QTextCodec来尝试将数据解码为GB2312,再编码回原始数据进行比较。如果一致,说明数据是GB2312编码。
  3. detectEncoding函数:读取文件内容,并使用isUtf8isGb2312函数来判断文件的编码格式。
  4. main函数:创建Qt应用程序实例,调用detectEncoding函数并输出文件的编码格式。

注意事项

  • 这种方法并不是百分百准确,因为某些字节序列在不同编码下可能都是合法的。因此,检测结果仅供参考。
  • 复杂的字符编码检测通常需要更复杂的算法或使用专门的库,如ICU(International Components for Unicode)。
  • 对于大文件,可以只读取文件的前几KB进行检测,以提高性能。
  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值