UTF-8与GBK互转:字符串和文件 C++实现

本文提供了一套C++代码,详细介绍了如何处理UTF-8和GBK编码之间的转换,包括字符串、文件及批量文件的互转,特别指出在Windows环境下文件名可能为GBK格式的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


平台:Windows/Linux
编辑器IDE:JetBrains Clion
第三方库:Boost

在操作文本文件的时候经常会遇到中文乱码的问题,尤其是在Windows平台下,本文针对常见的两种编码格式UTF-8和GBK编写一套代码实现二者的互转,可实现单字符串string,文件、批量文件、多级目录转换功能,详见代码:

1、头文件

//
// Created by lipin on 2019/12/18
//

#ifndef ENCODINGCONVERT_ENCODINGCONVERT_H
#define ENCODINGCONVERT_ENCODINGCONVERT_H

#include <iostream>
#include <string>
#include <Windows.h>
#include <fstream>
#include <vector>
#include "boost/filesystem.hpp"
namespace boostfs = boost::filesystem;

#define BUFFER_SIZE (2 * 1024 * 1024)  // allow file max size 2M
#define BUFFER_SIZE_ONE_LINE 1024

class EncodingConvert
{
   
public:
    EncodingConvert(const std::string &source_files_path_, const std::string &dest_files_path_):source_files_path(source_files_path_)
    ,dest_files_path(dest_files_path_)
    {
   
        // append \ to dest_files_path
        size_t separator = dest_files_path.find_last_of("\\");
        if(separator != dest_files_path.length() -1)
        {
   
            dest_files_path.append("\\");
        }
        boostfs::path path(dest_files_path);
        if(boostfs::exists(path) == false)  // create directory
        {
   
            boostfs::create_directories(path);
        }
    }

    void convertToUTF8();
    void convertToGBK();
    void convert(const std::string &input_file_path_, size_t source_encode_type_, size_t dest_encode_type_);
    static std::string convertString(size_t convert_type, const std::string source_string_);  // 转换方向 0---GBK to UTF-8  1--- UTF-8 to GBK

private:
    std::string source_files_path;
    std::string dest_files_path;
    std::vector<std::string> vct_file_paths;

    bool setFilePathSet(const std::string &file_set_path_);
};

#endif //ENCODINGCONVERT_ENCODINGCONVERT_H

2、源文件

//
// Created by lipin on 2019/12/18
//

#include <stdio.h>
#include "../encodingconvert.h"

bool EncodingConvert::setFilePathSet(const std::string &file_set_path_)
{
   
    std::cout << "scan file" << std::endl;
    boostfs::path path(file_set_path_);
    if(boostfs::exists(path) == false)
    {
   
        std::cout<<"ERROR: invalid path"<<std::endl;
        return false;
    }
    boostfs::directory_iterator end_iter;
    for(boostfs::directory_iterator iter(path); iter != end_iter 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值