STRING转WCHAR 和WCHAR 转STRING
2016-04-14 10:10
232 查看
// testvsprintf.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <windows.h> #include <string> // wchar_t to string void Wchar_tToString(std::string& szDst, wchar_t *wchar) { wchar_t * wText = wchar; DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用 char *psText; // psText为char*的临时数组,作为赋值给std::string的中间变量 psText = new char[dwNum]; WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用 szDst = psText;// std::string赋值 delete []psText;// psText的清除 } // string to wstring void StringToWstring(std::wstring& szDst, std::string str) { std::string temp = str; int len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, NULL,0); wchar_t * wszUtf8 = new wchar_t[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1, (LPWSTR)wszUtf8, len); szDst = wszUtf8; std::wstring r = wszUtf8; delete[] wszUtf8; } int main(int argc, char *argv) { setlocale(LC_ALL,"Chinese-simplified"); // string to wstring std::string szSrc = "字符串转换宽字符实例!OK!"; std::wstring wszDest; StringToWstring(wszDest, szSrc); std::wcout << "wszDest: " << wszDest << std::endl; }
相关文章推荐
- Webx是什么?
- Android.mk 详解
- 基于JZ2440的QT4移植
- HTTP 405 错误 – 方法不被允许 (Method not allowed)
- Android中的消息通知(NotificationManager和Notification)
- NetScaler VPX在Azure上的部署(二)
- android 多线程(二)
- The user specified as a definer ('wx_root'@'%') does not exist 解决方案
- 如何计算时间复杂度
- 10年后编程还有意义吗?
- activiti自定义流程之整合(四):整合自定义表单部署流程定义
- iprobe安装文档
- 自定义控件-下拉刷新和上拉加载的listView
- Android学习(21) -- Android版本号和API Level对应关系
- html加强
- 项目开发规范
- Ext与天地图的结合问题
- Linux下的iwpriv、iwlist、iwconfig
- 如何点击链接直接跳转到app store指定应用下载页面
- mysql 删除单表内多个字段重复的数据