这文章是更改别人代码

#include <string>
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <locale>
#include <locale.h>
#define _A_WIN //如果你是windows
using 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

C++宽字符串转字符串的更多相关文章

  1. python字符串、字符串处理函数及字符串相关操作

    python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...

  2. JS字符串替换函数:Replace(“字符串1″, “字符串2″),

    JS字符串替换函数:Replace(“字符串1″, “字符串2″), 1.我们都知道JS中字符串替换函数是Replace(“字符串1″, “字符串2″),但是这个函数只能将第一次出现的字符串1替换掉, ...

  3. c#.net常用字符串函数 字符串常用方法

    RegionsStr = RegionsStr.Remove(RegionsStr.LastIndexOf(","), 1);   //去掉最后一个逗号 Compare 比较字符串 ...

  4. Javascript里,想把一个整数转换成字符串,字符串长度为2

    Javascript里,想把一个整数转换成字符串,字符串长度为2.  想把一个整数转换成字符串,字符串长度为2,怎么弄?比如 1 => "01"11 => " ...

  5. 第一百二十七节,JavaScript,JSON数据类型转换,数据转换成字符串,字符串转换成数据

    第一百二十七节,JavaScript,JSON数据类型转换,数据转换成字符串,字符串转换成数据 学习要点: 1.JSON语法 2.解析和序列化 前两章我们探讨了XML的结构化数据,但开发人员还是觉得这 ...

  6. [ACdream]女神教你字符串——导字符串

    Problem Description 正如大家知道的,女神喜欢字符串,而在字符串中,女神最喜欢回文字符串,但是不是所有的字符串都是回文字符串,但是有一些字符串可以进行“求导”来变成回文字符串. 字符 ...

  7. go golang 判断base64数据 获取随机字符串 截取字符串

    go golang 判断base64数据 获取随机字符串 截取字符串 先少写点,占个坑,以后接着加. 1,获取指定长度随机字符串 func RandomDigits(length int) strin ...

  8. jquery获取当前按钮、截取字符串、字符串拼接、动态循环添加元素

    截取字符串:字符串拼接:动态循环添加元素:获取当前按钮: {data : null, render: function(data, type, row ) { var loginName = $(&q ...

  9. 剑指offer 1,输入一个字符串,将字符串的空格替换成%20

    剑指offer 1,输入一个字符串,将字符串的空格替换成%20    function replaceSpace(str){      return str.replace(/\s/g,"% ...

  10. C++二进制字符串转Base64字符串 Base64字符串转二进制字符串

    封装成类的 . base64格式的字符串,只包含大小写字母.零到九,以及 + / //___base_64.h /*base_64.h文件*/ #ifndef BASE_64_H #define BA ...

随机推荐

  1. Manacher模板( 线性求最长回文子串 )

    模板 #include<stdio.h> #include<string.h> #include<algorithm> #include<map> us ...

  2. 【BZOJ2460】元素(拟阵)

    题意:给定n个物品,每个物品有属性x和价值y,要求从中选出一些使得价值和最大并且其中没有属性xor和为0的非空子集 n<=1000,x<=1e18,y<=1e4 思路:没有xor和为 ...

  3. [LOJ2288][THUWC2017]大葱的神力:搜索+背包DP+费用流+随机化

    分析 测试点1.2:搜索+剪枝. 测试点3:只有一个抽屉,直接01背包. 测试点4.5:每个物品体积相同,说明每个抽屉能放下的物品个数固定,建图跑费用流. 测试点6:每个物品体积相近,经过验证发现每个 ...

  4. 进程(process)和线程(thread)

    1.计算机的核心是CPU,它承担了所有的计算任务.它就像一座工厂,时刻在运行. 2.假定工厂的电力有限,一次只能供给一个车间使用.也就是说,一个车间开工的时候,其他车间都必须停工.背后的含义就是,单个 ...

  5. 常用的HTML标记整理

    文章CSDN地址:https://blog.csdn.net/Ght1997... 文章GitHub地址:https://github.com/ght1997012...文章segmentfault地 ...

  6. 关闭Linux无用端口

    关闭系统不必要的端口,增强系统安全,此处以关闭111端口为例进行说明. 1).查看本机正在监听的端口: [root@b ~]# netstat -tlnup Active Internet conne ...

  7. duliu题之狼抓兔子题解

    拖了将近5天的正解和AC.........emmmmm........... 事实告诉我们这种毒瘤题一定要建双向边(用了不知道多少个小时质疑建边的人欲哭无泪) 心态爆炸的传送 题了个面 这是个求最小割 ...

  8. leetcode 374猜数字大小

    // Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...

  9. jenkins执行 pod install 报错 CocoaPods requires your terminal to be using UTF-8 encoding. Consider adding the following to ~/.profile:

    错误提示是: CocoaPods 需要终端使用utf-8编码 解决办法

  10. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_6_字符输出流写数据的其他方法

    从1开始写写三个字符 最后多了个bcd 写入字符串 字符串的一部分