在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下:

//将wstring转换成string
std::string ConvertWStringToAnsi(std::wstring wstr)
{
std::string result;
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
if( len <= 0 )
return result; char* buffer = new char[len + 1];
if(buffer == NULL )
return result; WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0'; //字符串断尾
result.append(buffer); //赋值
delete[] buffer; //删除缓冲区 //返回值
return result;
} //将string转换成wstring
std::wstring ConvertAnsiToWString(std::string str)
{
std::wstring result; int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
if( len < 0 )
return result; wchar_t* buffer = new wchar_t[len + 1];
if( buffer == NULL )
return result; MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len); buffer[len] = '\0'; //字符串断尾
result.append(buffer); //赋值
delete[] buffer; //删除缓冲区 //返回值
return result;
}

  

VC++中 wstring和string的互相转换实现的更多相关文章

  1. java中Integer 和String 之间的转换

    java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...

  2. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  3. C#中char[]与string之间的转换

    string 转换成 Char[] string ss = "abcdefg"; char[] cc = ss.ToCharArray(); Char[] 转换成string st ...

  4. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  5. VC++中有关句柄和指针及其转换(转)

    原文转自 https://blog.csdn.net/jearmy/article/details/47030011 1.MFC窗口的句柄和指针的转换 (1) 一般窗口对象都会有一个其对应的句柄变量, ...

  6. c++ 中double与string之间的转换,char *

    运行代码为 /* * main.cpp * * Created on: Apr 7, 2016 * Author: lizhen */ #include <iostream> //#inc ...

  7. C++中int,float,string,char*的转换(待续)

    //float转string char a[100]; float b = 1.234; sprintf(a, "%f", b); string result(a); //int转 ...

  8. python3中bytes与string的互相转换

    首先来设置一个原始的字符串, Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Ty ...

  9. Java中int和String类型之间转换

    int –> String int i=123; String s=""; 第一种方法:s=i+""; //会产生两个String对象 第二种方法:s=S ...

随机推荐

  1. SQL中EXISTS和IN用法

    SQL中EXISTS的用法  指定一个子查询,检测行的存在. 语法:EXISTS subquery 参数:subquery 是一个受限的 SELECT 语句 (不允许有 COMPUTE 子句和 INT ...

  2. JQuery 去除字符串两边多余的空格

    var str = "  hello  "; str = $.trim(str);//去除多余空格 //变成了"hello"

  3. HttpResponseCache 网络缓存使用

    Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. ...

  4. Learning WCF Chapter1 Hosting a Service in IIS

    How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...

  5. USACO3.35A Game(记忆化)

    刚开始理解有点误,想成每步都是最优的 ,结果卡在第六组数据上,, 无奈瞧了下别人的 发现自己理解错了,,我看的还是中文翻译.. 简单的记忆化 /* ID: shangca2 LANG: C++ TAS ...

  6. C#面向对象——方法的重载及构造函数、静态对象。

    namespace nameclass2 { class Class2 //方法的重载 { public void Function( string s) { Console.WriteLine(s) ...

  7. spoolsv.exe 报错,无法打印

    在使用打印机过程中突然出现spoolsv.exe应用程序错误,内存不能written•••,检查打印驱动,打印机设置选项无法打开.怀疑是病毒所致,升级杀毒软件后安全模式下杀毒后没有发现病毒,重启后还是 ...

  8. eclipse环境NDK问题汇总

    1. 配置NDK路径设置 可以在cygwin中通过vim修改,也可以在windows安装目录中修改 home\<你的用户名>\.bash_profile 文件中最后添加环境变量 NDK=/ ...

  9. Excel和XML文件导入

    using System;using System.Collections;using System.Collections.Generic;using System.Configuration;us ...

  10. TFS2010中文版安装

    VS2010的中文版出来一段时间了,对TFS2010的了解,也有一段时间了,只不过中文版还是首次见到.于是把第一次安装的图片分享出来,公供参数. TFS2010安装环境是操作系统为Windows Se ...