C++字符串编码GBK与UTF-8转换

c库

https://www.tutorialspoint.com/c_standard_library/string_h.htm

ASCII对照表

https://tool.oschina.net/commons?type=4

 

以UTF-8转GBK为例,其他原理相同

1. 列出当前系统支持的locale:  locale -a |grep utf-8 gbk

2. utf-8和gbk的locale的副本
locale loc_utf8("zh_CN.UTF-8");
locale loc_gbk("zh_CN.GBK");

3. 

string s("你们好呀呀呀哎呀或或");      // utf8 string

wstring ws = s2ws(s, loc_utf8);          // utf8string to ws
string a = ws2s(ws, loc_gbk);             // ws 2 gbk

 

4. 这样就能将string a输出了,a就是编码为gbk的multi byte string

 

https://www.gnu.org/software/libunistring/manual/libunistring.html

posted on   heysong  阅读(781)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示