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 2023-06-02 22:33  heysong  阅读(746)  评论(0编辑  收藏  举报

导航