//StringConvert.h
#ifndef STRINGCONVERT_H_H
#define STRINGCONVERT_H_H
#include "stdafx.h"
#include
#include
using std::string;
using std::wstring;
wstring AsciiToUnicode(const string& str);
string AsciiToUtf8(const string& str);
string UnicodeToAscii(const wstring& wstr);
string UnicodeToUtf8(const wstring& wstr);
string Utf8ToAscii(const string& str);
wstring Utf8ToUnicode(const string& str);
#endif
//StringConvert.cpp
#include "stdafx.h"
#include "StringConvert.h"
wstring AsciiToUnicode(const string& str)
{
//得到str的字节数
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
wchar_t * wstr = new wchar_t[sizeof(wchar_t)*len];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, len);
wstring destr = wstr;
delete[] wstr;
return destr;
}
string AsciiToUtf8(const string& str)
{
wstring wstr=AsciiToUnicode(str);
string destr = UnicodeToUtf8(wstr);
return destr;
}
string UnicodeToAscii(const wstring& wstr)
{
// 得到wstr的自己数
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
char* str = new char[sizeof(char)*len];
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, str, len, nullptr, nullptr);
string destr = str;
delete[] str;
return destr;
}
string UnicodeToUtf8(const wstring& wstr)
{
// 得到wstr的字节数
int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
char* str = new char[sizeof(char)*len];
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, str, len, nullptr, nullptr);
string destr = str;
delete[] str;
return destr;
}
string Utf8ToAscii(const string& str)
{
wstring wstr = Utf8ToUnicode(str);
string destr = UnicodeToAscii(wstr);
return destr;
}
wstring Utf8ToUnicode(const string& str)
{
//得到str的字节数
int len = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,nullptr,0);
wchar_t* wstr = new wchar_t[sizeof(wchar_t)*len];
MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,wstr,len);
wstring destr = wstr;
delete[] wstr;
return destr;
}