#include "stdafx.h"
#include <string>
#include <iostream>
#include <windows.h>
using std::string;
using std::wstring;
string UnicodeToAscii(const wstring& wstr) ;
wstring AsciiToUnicode(const string& str);
int _tmain(int argc, _TCHAR* argv[])
{
//去全角空格
string str = " 你 you " ,destr="";
wstring wstr = AsciiToUnicode(str);
size_t pos = wstr.find_first_of(_T(" "));
while(string::npos != pos)
{
wstr.replace(pos,sizeof(wchar_t),_T(""));
pos = wstr.find_first_of(_T(" "));
}
destr = UnicodeToAscii(wstr);
return 0;
}
string UnicodeToAscii(const wstring& wstr)
{
// wstr的长度
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
//准备内存放置转换后的窄字符串
char *str = (char*)malloc(sizeof(char)*len);
// 宽转窄,写入str中
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, len, nullptr, nullptr);
string destr = str;
free(str);
return destr;
}
wstring AsciiToUnicode(const string& str)
{
// str转为wstring需要的长度
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
// 准备内存放置转换后的宽字符串
wchar_t *wstr = (wchar_t*)malloc(sizeof(wchar_t)*len);
// 窄转宽,写入wstr中
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, len);
wstring deswstr = wstr;
free(wstr);
return deswstr;
}