//宽字符转多字节
std::string W2A(const std::wstring& utf8)
{
int buffSize = WideCharToMultiByte(CP_ACP, NULL, utf8.c_str(), -1, NULL, NULL, NULL, FALSE);
char *gbk = new char[buffSize+1];
memset(gbk, 0, buffSize + 1);
WideCharToMultiByte(CP_ACP, NULL, utf8.c_str(), -1, gbk, buffSize, NULL, FALSE);
std::string result(gbk);
delete[] gbk;
gbk = nullptr;
return result;
} //多字节转宽字符
std::wstring A2W(const std::string& gbk)
{
int buffSize = MultiByteToWideChar(CP_ACP, NULL, gbk.c_str(), -1, NULL, NULL);
wchar_t* utf8 = new wchar_t[buffSize+2];
memset(utf8, 0, buffSize + 2);
MultiByteToWideChar(CP_ACP, NULL, gbk.c_str(), -1, utf8, buffSize);
std::wstring result(utf8);
delete[] utf8;
utf8 = nullptr;
return result;
}

如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)的更多相关文章

  1. 如何使用 window api 转换字符集?

    //宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...

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

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

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

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

  4. C++ struct结构体定义构造函数和析构函数,构造函数参数从VS2017平台转换到Qt5平台下构建出错,采用字符集转换函数将string类型转换为wstring,构建仍然出错!

    调试win硬件驱动,需要利用VS编译的win驱动构建自己的Qt5GUI程序: 其中部分win驱动源码如下 device_file::device_file(const std::string& ...

  5. std::string与std::wstring互相转换

    作者:zzandyc来源:CSDN原文:https ://blog.csdn.net/zzandyc/article/details/77540056 版权声明:本文为博主原创文章,转载请附上博文链接 ...

  6. std::string和int类型的相互转换(C/C++)

    字符串和数值之前转换,是一个经常碰到的类型转换. 之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考: 优缺点:C++的stringstream智 ...

  7. std::u32string conversion to/from std::string and std::u16string

    I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...

  8. std::string 用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

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

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

随机推荐

  1. android:targetSdkVersion引起的问题

    项目在三星S3和三星Note II 上调用系统相机点击存储的时候崩溃了.查了半天没弄明白原因,后来发现就是因为在manifest里设置了android:targetSdkVersion = 14,导致 ...

  2. Java如何根据主机名(域名)获取IP地址?

    在Java编程中,如何根据主机名(域名)获取IP地址? 以下示例显示了如何通过net.InetAddress类的InetAddress.getByName()方法将主机名更改为指定的IP地址. pac ...

  3. net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting head

    使用docker 拉镜像的时候,出现下面的错误: net/http: request canceled while waiting for connection (Client.Timeout exc ...

  4. umi怎么去添加配置式路由

    今天在学习umi,他的路由机制非常的方便,但是在学到配置式路由的时候,看官方文档里面一笔带过: 对于我这种小萌新来说,有点懵,我需要把配置文件放到哪里呢?经过一番研究,发现它是放在根目录的.umirc ...

  5. linux 几个命令

    tail: tail -20 xxx  --查看xxx文件的最后20行 more:分页查看,只能向后,不能向前 less:查看文件,可向前,向后,用的比较多 ll -h|more:当文件较多时,可以通 ...

  6. twisted 学习笔记一:事件循环

    from twisted.internet import reactor import time def printTime(): print "Current time is", ...

  7. springCloud学习之服务注册和发现

    leader让完一个简单的springcloud的demo,自己之前听说过springcloud微服务,但是没有重视.现在网上查各种资料,但是感觉不怎么样啊,还是不会,明天晚上把代码给他看,天啦,这个 ...

  8. Django之MVC框架与MTV框架详解

    Django框架简介 MVC框架和MTV框架(了解即可) MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图 ...

  9. simulation vs emulation

    Hardware emulation, the use of special purpose hardware to emulate the behavior of a yet-to-be-built ...

  10. 新复制行绑定JQuery.autocomplete事件

    在工作中经常需要生成动态增加行的表格,此次需要对表格中的文本框绑定autocomplete功能 操作流程: 表格初始时只有一行,当页面加载时执行先执行一次增加行功能,将行增加到10行 在$(funct ...