C++ – Windows/Linux跨平台gbk与utf8字符集编码转换
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;
}
参考链接
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – Windows/Linux跨平台gbk与utf8字符集编码转换
原文链接:https://www.stubbornhuang.com/2524/
发布于:2023年02月27日 17:37:17
修改于:2023年06月21日 17:08:26
当前分类随机文章推荐
- C++ - 判断本机文件是否存在的方式总结 阅读6154次,点赞0次
- C++ - std::string与std::wstring相互转换 阅读2449次,点赞0次
- C++ - 动态链接库dll为什么要使用unsigned char作为byte的内部格式 阅读987次,点赞0次
- C++11 - 使用std::thread::join()/std::thread::detach()方法需要注意的点 阅读3527次,点赞0次
- C++ - 导出接口函数和导出C++类 阅读855次,点赞0次
- C++ - std::string替换字符串中所有指定的子字符串 阅读4099次,点赞1次
- C++ - websocket++库的可使用的所有事件总结 阅读423次,点赞0次
- C++ - const_cast, static_cast, dynamic_cast, reinterpret_cast四种cast转换的区别和使用 阅读102次,点赞0次
- C++ - 函数返回多个返回值的方法总结 阅读2608次,点赞0次
- 计算几何 - C++计算两个二维向量的夹角 阅读5297次,点赞3次
全站随机文章推荐
- 资源分享 - Learn OpenGL - Learn modern OpenGL graphics programming in a step-by-step fashion 英文高清PDF下载 阅读1667次,点赞0次
- 资源分享 - A Biography of the Pixel 英文高清PDF下载 阅读2589次,点赞0次
- 资源分享 - Game AI Pro 3 - Collected Wisdom of Game AI Professionals 英文高清PDF下载 阅读2756次,点赞0次
- 资源分享 - Game Programming Gems 3 英文高清PDF下载 阅读2419次,点赞0次
- 我的开源项目 - 使用OnnxRuntime在CPU端部署RTMPose玩转实时2D姿态估计 阅读839次,点赞1次
- Visual Studio - 将程序的日志输出到Visual Studio即时窗口 阅读5556次,点赞1次
- 资源分享 - Game Engine Architecture (Second Edition)英文高清PDF下载 阅读2677次,点赞0次
- Python - 解决skvideo库的Cannot find installation of real FFmpeg (which comes with ffprobe)问题 阅读503次,点赞0次
- OnnxRuntime - 模型部署笔记1,OnnxRuntime简介 阅读838次,点赞0次
- Centos7 - 配置Go环境 阅读3076次,点赞1次
评论
169