zoukankan      html  css  js  c++  java
  • VC++中 wstring和string的互相转换实现

    在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] = '';               //字符串断尾
    	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] = '';                    //字符串断尾
    	result.append(buffer);                 //赋值
    	delete[] buffer;                       //删除缓冲区
    
    	//返回值
    	return result;
    }
    

      

  • 相关阅读:
    python+selenium初学者常见问题处理
    pycharm的这些配置,你都知道吗
    巧用浏览器F12调试器定位系统前后端bug
    dsu + lca
    indeed2017校招在线编程题(网测)三
    rolling hash
    ac自动机
    indeed 第二次笔试题
    vmware以及schlumberger题解
    2017 google Round C APAC Test 题解
  • 原文地址:https://www.cnblogs.com/JczmDeveloper/p/3527189.html
Copyright © 2011-2022 走看看