C++ – Windows/Linux跨平台gbk与utf8字符集编码转换
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – Windows/Linux跨平台gbk与utf8字符集编码转换
原文链接:https://www.stubbornhuang.com/2524/
发布于:2023年02月27日 17:37:17
修改于:2023年02月27日 17:39:53

1 Windows/Linux跨平台gbk与utf8字符集编码转换
在Windows系统上使用系统api,在linux上使用iconv进行gbk与utf8字符集编码转换
我已经将上述功能封装为single header的工具类,具体代码如下
#ifndef _CHARACTERSET_CONVERT_H_
#define _CHARACTERSET_CONVERT_H_
#include <string>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include <Windows.h>
#elif defined(linux) || defined(__linux)
#include <iconv.h>
#include <malloc.h>
#endif
namespace stubbornhuang
{
class CharactersetConvert
{
public:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
static std::string gbk_to_utf8(const std::string& gbk_str)
{
int len = MultiByteToWideChar(CP_ACP, 0, gbk_str.c_str(), -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gbk_str.c_str(), -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
std::string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
static std::string utf8_to_gbk(const std::string& utf8_str)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}
#elif defined(linux) || defined(__linux)
static int code_convert(
const char* from_charset,
const char* to_charset,
char* inbuf, size_t inlen,
char* outbuf, size_t outlen
) {
iconv_t cd;
char** pin = &inbuf;
char** pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
if (cd == 0)
return -1;
memset(outbuf, 0, outlen);
if ((int)iconv(cd, pin, &inlen, pout, &outlen) == -1)
{
iconv_close(cd);
return -1;
}
iconv_close(cd);
*pout = '\0';
return 0;
}
static int u2g(char* inbuf, size_t inlen, char* outbuf, size_t outlen) {
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}
static int g2u(char* inbuf, size_t inlen, char* outbuf, size_t outlen) {
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);
}
static std::string gbk_to_utf8(const std::string& gbk_str)
{
int length = gbk_str.size() * 2 + 1;
char* temp = (char*)malloc(sizeof(char) * length);
if (g2u((char*)gbk_str.c_str(), gbk_str.size(), temp, length) >= 0)
{
std::string str_result;
str_result.append(temp);
free(temp);
return str_result;
}
else
{
free(temp);
return "";
}
}
static std::string utf8_to_gbk(const std::string& utf8_str)
{
int length = strlen(utf8_str);
char* temp = (char*)malloc(sizeof(char) * length);
if (u2g((char*)utf8_str, length, temp, length) >= 0)
{
std::string str_result;
str_result.append(temp);
free(temp);
return str_result;
}
else
{
free(temp);
return "";
}
}
#endif
};
}
#endif // !_CHARACTERSET_CONVERT_H_
使用示例
#include <iostream>
#include "CharactersetConvert.h"
using namespace std;
int main()
{
std::string nihao = "你好";
std::string nihao_utf8 = stubbornhuang::CharactersetConvert::gbk_to_utf8(nihao);
std::cout << nihao_utf8 << std::endl;
std::string nihao_gbk = stubbornhuang::CharactersetConvert::utf8_to_gbk(nihao_utf8);
std::cout << nihao_gbk << std::endl;
return 0;
}
参考链接
当前分类随机文章推荐
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读3866次,点赞2次
- C++ - Windows/Linux跨平台gbk与utf8字符集编码转换 阅读98次,点赞0次
- C++ - C++类的特殊成员函数,析构函数,拷贝构造函数,移动构造函数,赋值运算符,移动赋值运算符介绍和基础语法 阅读758次,点赞0次
- C++ - 将std::vector中的数值拷贝到数组中 阅读2140次,点赞1次
- C++ 回调函数 阅读2907次,点赞0次
- C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点 阅读2700次,点赞0次
- C++ - 常用的C++命令行参数解析第三方库 阅读2336次,点赞2次
- C++STL容器 – std::vector容器修改、元素操作总结 push_back,emplace_back,emplace,insert,erase,pop_back,clear,resize,swap 阅读907次,点赞1次
- C++ - 使用标准库std::use_facet和std::codecvt进行跨平台gbk与utf8字符集转换 阅读91次,点赞0次
- Centos7 编译C++项目错误解决 : terminate called after throwing an instance of 'std::regex_error' 阅读2434次,点赞1次
全站随机文章推荐
- 深度学习 - 通俗理解Beam Search Algorithm算法 阅读638次,点赞0次
- Python3爬虫 - requests库 阅读3639次,点赞3次
- 资源分享 - Light & Skin Interactions - Simulations for Computer Graphics Applications 英文高清PDF下载 阅读1307次,点赞0次
- 资源分享 - Graphics Shaders - Theory and Practice (Second Edition) 英文高清PDF下载 阅读2227次,点赞0次
- WordPress - 禁止非管理员登录后台 阅读1680次,点赞0次
- 深度学习 - Transformer详细注释 阅读417次,点赞0次
- 资源分享 - OpenGL 4.0 Shading Language Cookbook (Third Edition) 英文高清PDF下载 阅读2798次,点赞0次
- 资源分享 - Game Programming Gems 1 英文高清PDF下载 阅读2101次,点赞0次
- 资源分享 - Digital Modeling of Material Appearance 英文高清PDF下载 阅读1442次,点赞0次
- Python - 写爬虫时需要用到那些第三方库 阅读350次,点赞0次
评论
167