使用Windows API或iconv库实现C++中UTF-8与GBK互转,Windows通过WideCharToMultiByte等函数以UTF-16为中介转换,Linux下用iconv库处理,跨平台可选ICU或封装统一接口。

在C++中处理中文字符时,经常会遇到UTF-8和GBK编码之间的转换需求,尤其是在跨平台开发或与Windows系统交互时。由于C++标准库本身不直接支持多字节编码转换,我们需要借助第三方库或系统API来实现。
在Windows平台上,可以使用MultiByteToWideChar和WideCharToMultiByte函数完成编码转换,通过UTF-16作为中间编码进行中转。
UTF-8 转 GBK 示例:
#include <windows.h>
#include <string>
<p>std::string utf8_to_gbk(const std::string& utf8) {
int len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, nullptr, 0);
if (len == 0) return "";</p><pre class='brush:php;toolbar:false;'>std::wstring wide(len, 0);
MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide[0], len);
len = WideCharToMultiByte(936, 0, wide.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (len == 0) return "";
std::string gbk(len - 1, 0);
WideCharToMultiByte(936, 0, wide.c_str(), -1, &gbk[0], len, nullptr, nullptr);
return gbk;}
立即学习“C++免费学习笔记(深入)”;
GBK 转 UTF-8 示例:
std::string gbk_to_utf8(const std::string& gbk) {
int len = MultiByteToWideChar(936, 0, gbk.c_str(), -1, nullptr, 0);
if (len == 0) return "";
<pre class='brush:php;toolbar:false;'>std::wstring wide(len, 0);
MultiByteToWideChar(936, 0, gbk.c_str(), -1, &wide[0], len);
len = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, nullptr, 0, nullptr, nullptr);
if (len == 0) return "";
std::string utf8(len - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, &utf8[0], len, nullptr, nullptr);
return utf8;}
立即学习“C++免费学习笔记(深入)”;
在Linux或macOS系统中,推荐使用iconv库进行编码转换,它支持多种编码格式且跨平台兼容性好。
安装 iconv(如未自带):
# Ubuntu/Debian
sudo apt-get install libiconv-dev
<h1>macOS (使用Homebrew)</h1><p>brew install libiconv</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/2121">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175680148482336.png" alt="AI图像编辑器">
</a>
<div class="aritcle_card_info">
<a href="/ai/2121">AI图像编辑器</a>
<p>使用文本提示编辑、变换和增强照片</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="AI图像编辑器">
<span>46</span>
</div>
</div>
<a href="/ai/2121" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="AI图像编辑器">
</a>
</div>
使用 iconv 进行转换:
#include <iconv.h>
#include <string>
<p>std::string code_convert(const std::string& in, const char<em> from, const char</em> to) {
iconv_t cd = iconv_open(to, from);
if (cd == (iconv_t)-1) return "";</p><pre class='brush:php;toolbar:false;'>size_t in_len = in.length();
size_t out_len = in_len * 4;
std::string out(out_len, 0);
char* in_buf = const_cast<char*>(in.c_str());
char* out_buf = &out[0];
size_t ret = iconv(cd, &in_buf, &in_len, &out_buf, &out_len);
iconv_close(cd);
if (ret == (size_t)-1) return "";
out.resize(out.length() - out_len);
return out;}
立即学习“C++免费学习笔记(深入)”;
// 使用示例 std::string utf8_to_gbk(const std::string& utf8) { return code_convert(utf8, "UTF-8", "GBK"); }
std::string gbk_to_utf8(const std::string& gbk) { return code_convert(gbk, "GBK", "UTF-8"); }
如果需要更轻量或更强大的支持,可以选择:
ICU 示例片段:
#include <unicode/ucnv.h>
<p>std::string ucnv_convert(const char<em> from_encoding, const char</em> to_encoding, const std::string& input) {
UErrorCode err = U_ZERO_ERROR;
UConverter<em> from = ucnv_open(from_encoding, &err);
UConverter</em> to = ucnv_open(to_encoding, &err);</p><pre class='brush:php;toolbar:false;'>int32_t target_len = ucnv_toAlgorithmic(UCNV_UTF8, to, nullptr, 0,
ucnv_getUnicodeSet(from, nullptr, &err), input.c_str(), input.length(), &err);
// 实际转换略,需分配缓冲区并调用 ucnv_convertEx
// 此处简化说明,具体参考 ICU 文档
ucnv_close(from); ucnv_close(to);
return ""; // 省略完整实现}
立即学习“C++免费学习笔记(深入)”;
编译时需链接:
-licuuc -licudata
基本上就这些。根据你的运行环境选择合适的方法,Windows用API,Linux用iconv,复杂需求上ICU。不复杂但容易忽略细节。
以上就是c++++中如何进行UTF-8和GBK编码转换_C++字符编码转换方案的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号