最近在处理一个字符串转码问题,故记录一下过程

该需求是外部 sdk 的一个 api 需要一个 char* 字符串路径入参,我以往是将宽字符串转为 UTF8 后再传给 sdk

这次这个 api 似乎不接受 UTF8 编码的字符串路径,于是我改用系统编码传参作测试,也就是将 GB2312 编码的字符串路径传给它

结果显示该 api 只接受本地编码的字符串路径,因此我要处理宽字符串转为系统编码的问题

我在 stackoverflow 论坛上搜到一些答案

其中有一个方法要注意避坑,

这是个高赞答案,链接:https://stackoverflow.com/a/12097772/11128312

但是这个只适用于英文字符,因为这种转换相当于把宽字节的第二个字节抹除了,比如,

“帮助” 的 unicode 编码为:2e 5e a9 52

使用上图的方法就给你干掉宽字节的第二个字节,

变成:2e a9

这是一个四不像字符

英文字符就不一样了,比如

“he” 的 unicode 编码为:68 00 65 00

用 std::string 就可以去掉 00,

变成:68 65

故我们要用其他方法来转换,方法很多,我这里列举两个,

C++17 后支持 std::filesystem::path 直接转,示例,

#include <filesystem>

const std::wstring wPath = GetPath(); // some function that returns wstring
const std::string path = std::filesystem::path(wPath).string();

也可以使用 WcToMb 工具函数

WcToMb 原型:

char* WcToMb(const wchar_t *str)
{
char *mbstr = NULL;
lo_W2C(&mbstr, str);
return mbstr;
}

lo_W2C 内部也是用的 WideCharToMultiByte 函数转换的

lo_W2C 函数原型

    /*
** Convert microsoft unicode to multibyte character string, based on the
** user's Ansi codepage.
**
** Space to hold the returned string is obtained from
** malloc().
*/
int lo_W2C(char** pout ,const wchar_t *zWide)
{
#if (defined(WIN32) || defined(_WIN32) )
char *zname = 0;
int codepage = 0;
int nByte = 0; if( !zWide || *zWide == '\0' )
return 0; #if defined(_WIN32_WCE)
codepage = CP_ACP ;
#else
codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
#endif
nByte = WideCharToMultiByte(codepage, 0, zWide, -1, 0, 0, 0, 0);
zname = (char*)malloc( nByte + 1 );
if( zname == 0 ) return 0; nByte = WideCharToMultiByte(codepage, 0, zWide, -1, zname, nByte+1, 0, 0);
if( nByte > 0 ) zname[nByte] = '\0'; *pout = zname;
return nByte;
#else const wchar_t* in = zWide;
size_t in_len = wcslen(in);
size_t outlen = bbiconv_counts_wchar_2_gbk(in , in_len);
if( outlen <= 0 )
return -1; char* out_ptr = (char*)malloc( (sizeof(char)) * (outlen+1) );
if( !out_ptr )
return -1; bbiconv_wchar_2_gbk(in , in_len , out_ptr , outlen);
*pout = out_ptr;
return outlen; #endif //#if (defined(WIN32) || defined(_WIN32) ) }

补充:

std::wstring 和 wchar_t 内部中文编码是按照 unicode 编码的
std::string 和 char 内部中文编码是按照系统字符集编码,一般中文是 GB2312 字符集

另附:

std::string std::wstring char w_char 内部中文编码的更多相关文章

  1. std::string stringf(const char* format, ...)

    std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 ...

  2. How to convert a std::string to const char* or char*?

    How to convert a std::string to const char* or char*? 1. If you just want to pass a std::string to a ...

  3. std::string, std::wstring, wchar_t*, Platform::String^ 之间的相互转换

    最近做WinRT的项目,涉及到Platform::String^  和 std::string之间的转换,总结一下: (1)先给出源代码: std::wstring stows(std::string ...

  4. 为什么推荐std::string而不是char*

    例如如下: map<const char*, const char*> map_test; map_test["a"] = "a"; map_tes ...

  5. std::string std::wstring 删除最后元素 得到最后元素

    std::string str = "abcdefg,"; std::cout << "last character:"<<str.ba ...

  6. std::string,std::vector,std::accumulate注意事项

    在用string做字符串拼接时,会发现随着string的增大越来越慢,原因主要是string(和vector)是基于现行内存的数据结构,在海量数据时,经常会申请新的一块内存,把原有的数据拷贝过去然后再 ...

  7. C++ MFC std::string转为 std::wstring

    std::string转为 std::wstring std::wstring UTF8_To_UTF16(const std::string& source) { unsigned long ...

  8. 对std::string和std::wstring区别的解释,807个赞同,有例子

    807down vote string? wstring? std::string is a basic_string templated on a char, and std::wstring on ...

  9. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  10. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

随机推荐

  1. [转帖]nginx配置文件中对于if条件语句的写法(附nginx跨域文件配置)

    前言 在nginx配置文件中,可以使用if语句,但是对于else语句其实是不支持的,并且and条件和or条件也是不支持的 实现 else条件的写法 新建一个开关变量flag,初始值为0,如果为1说明进 ...

  2. [转帖]从小白到精通:揭秘perf工具的全部功能与操作技巧

    https://zhuanlan.zhihu.com/p/664396453 ​ 目录 收起 一.引言 二.理解perf工具的基本概念 三.安装与配置perf工具 3.1.不同操作系统的perf工具安 ...

  3. [转帖]shell脚本实现文本内容比较交互程序

    背景介绍 脚本基于Comm命令进行功能封装,考虑到命令执行前需要对文本进行排序,并且在多文件需要比较内容时可能会导致多个文本混乱,因此使用Shell封装成了一个交互式程序,快速对文件内容进行判断和输出 ...

  4. [转帖]clickHouse单机模式安装部署(RPM安装)

    关于版本和系统的选择 操作系统:Centos-7 ClickHouse: rpm 在安装,20.x 安装前的准备 CentOS7 打开文件数限 在 /etc/security/limits.conf ...

  5. [转帖]CPU缓存行

    https://www.jianshu.com/p/e338b550850f CPU缓存 执行程序是靠运行CPU执行主存中代码,但是CPU和主存的速度差异是非常大的,为了降低这种差距,在架构中使用了C ...

  6. Intel 的家用CPU的后缀

    Intel 也奸商 各种后缀 最开始P 表示无核显版本 现在用 F来表示了.. 略坑. 一下来源百家号: https://baijiahao.baidu.com/s?id=16276964705166 ...

  7. VScode中下载了插件但是无法找到SSH Target连接服务器的解决方法(CANNOT find SSH Target in remote explorer)

    VSCode版本vscode version:(version 1.82) 已下载扩展installed extensions: Remote - SSH v0.106.4 Remote - SSH: ...

  8. 防止xxs攻击,input表单中不能输入script标签

    在web网页中,所有的项目中.input表单中不能让用户输入script这些敏感性的. 一旦出现提示用户非正常输入.然后立刻将值清空 <el-input style="width:35 ...

  9. web端用户的输入都应该做如下限制

    web端中,所有可以输入的地方.都应该做如下操作. 1=>不能够输入script关键字,如果用户输入了.进行提示.然后删除用户输入的值 (这样比较粗暴,不太友好) 2=>用户输入了含有sc ...

  10. Python 实现ARP扫描与欺骗

    ARP欺骗又称ARP毒化或ARP攻击,是针对以太网地址解析协议ARP的一种攻击技术,通过欺骗局域网内访问者PC的网关MAC地址,使访问者PC错以为攻击者更改后的MAC地址是网关的MAC,导致网络不通. ...