C++宽字符串转字符串
来源:互联网 发布:配眼镜 知乎 编辑:程序博客网 时间:2024/06/05 17:17
这文章是更改别人代码
#include <string>#include <iostream>#include <stdlib.h>#include <windows.h>#include <locale>#include <locale.h>#define _A_WIN //如果你是windowsusing namespace std;//把字符串转换成宽字符串wstring string_wstring(string sToMatch){#ifdef _A_WIN int iWLen = MultiByteToWideChar( CP_ACP, 0, sToMatch.c_str(), sToMatch.size(), 0, 0 ); // 计算转换后宽字符串的长度。(不包含字符串结束符) wchar_t *lpwsz = new wchar_t [iWLen + 1]; MultiByteToWideChar( CP_ACP, 0, sToMatch.c_str(), sToMatch.size(), lpwsz, iWLen ); // 正式转换。 lpwsz[iWLen] = L'\0'; wstring wsToMatch(lpwsz); delete []lpwsz; return wsToMatch;#elifdef _A_LINUX setlocale( LC_CTYPE, "" ); // 很重要,没有这一句,转换会失败。 int iWLen = mbstowcs( NULL, sToMatch.c_str(), sToMatch.length() ); // 计算转换后宽字符串的长度。(不包含字符串结束符) wchar_t *lpwsz = new wchar_t[iWLen + 1]; int i = mbstowcs( lpwsz, sToMatch.c_str(), sToMatch.length() ); // 转换。(转换后的字符串有结束符) wstring wsToMatch(lpwsz); delete []lpwsz; return wsToMatch;#endif //return wsToMatch; return NULL;}//把宽字符串转换成字符串,输出使用string wstring_string(wstring sToMatch){#ifdef _A_WIN string sResult; int iLen = WideCharToMultiByte( CP_ACP, NULL, sToMatch.c_str(), -1, NULL, 0, NULL, FALSE ); // 计算转换后字符串的长度。(包含字符串结束符) char *lpsz = new char[iLen]; WideCharToMultiByte( CP_OEMCP, NULL, sToMatch.c_str(), -1, lpsz, iLen, NULL, FALSE); // 正式转换。 sResult.assign( lpsz, iLen - 1 ); // 对string对象进行赋值。 delete []lpsz; return sResult;#elifdef _A_LINUX int iLen = wcstombs( NULL, sToMatch.c_str(), 0 ); // 计算转换后字符串的长度。(不包含字符串结束符) char *lpsz = new char[iLen + 1]; int i = wcstombs( lpsz, sToMatch.c_str(), iLen ); // 转换。(没有结束符) lpsz[iLen] = '\0'; string sResult(lpsz); delete []lpsz; return sResult;#endif //return sResult; return NULL;}
更改
wstring string_wstring(string sToMatch):return wsToMatch;string wstring_string(wstring sToMatch):return sResult;wstring string_wstring(string sToMatch):lpwsz[iWLen] = L'\0';string wstring_string(wstring sToMatch):lpsz[iLen] = '\0';#elif:#elifdef;
参考:http://blog.csdn.net/stephen_yin/article/details/6292728
2 0
- C字符串转JNI字符串
- c 字符串 转大写
- c字符串转整数
- 字符串转整数(C#)
- c 整数转字符串
- C++: 整型转字符串
- c 字符转字符串
- C++int转字符串
- 【算法C++】十进制字符串转十六进制字符串
- 【转】C字符串和C++字符串
- [iOS 字符串] oc字符串与c字符串互转
- C字符串
- C++--字符串
- C字符串
- [C#] 字符串
- c字符串
- C++--字符串
- C-字符串
- FreeRTOS 特性简介
- JDK中的动态代理
- MyEclipse常用快捷键
- Android Studio系列教程一--下载与安装
- 统计数组中元素出现的次数——Java小程序
- C++宽字符串转字符串
- 实现的效果:EditText提示文本字体大小和输入文本字体大小不同
- Java中的Hashtable实现方法
- Android UI 之 SeekBar
- Android 实现能够暂停的录音功能
- Ogre切换摄像头
- Spring实例学习
- OC笔记-学习self时候2个target然后修改名字出现linker command failed with exit code 1 (use -v to see invocation)
- linux Centos iptables 8080