Windows平台编码转化,任何乱码问题,统统搞定,我一直使用这个:
头文件:
#pragma once
#include "stdafx.h"
#include <string>
//字符串各种转
class CCharCvt
{
public:
CCharCvt();
~CCharCvt();
public:
static bool AnsiToUnicode(const char* szAnsi, std::wstring& szUnicode);
static bool UnicodeToAnsi(const wchar_t* szUnicode, std::string& szAnsi);
static bool UnicodeToUTF8(const wchar_t* szUnicode, std::string& szUTF8);
static bool AnsiToUTF8(const char* szAnsi, std::string& szUTF8);
static bool UTF8ToUnicode(const char* szUTF8, std::wstring& strUnicode);
static bool UTF8ToAnsi(const char* szUTF8, std::string& strAnsi);
};
源文件:
#include "CCharCvt.h"
#include <memory>
using namespace std;
CCharCvt::CCharCvt(void)
{
}
CCharCvt::~CCharCvt(void)
{
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时40分
func : ANSI转Unicode
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::AnsiToUnicode(const char* szAnsi, wstring& szUnicode)
{
UINT nNeededLen = MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, NULL, 0);
unique_ptr<wchar_t[]> wszTmp(new wchar_t[nNeededLen]);
if(!wszTmp)
{
return false;
}
MultiByteToWideChar(CP_ACP, 0, szAnsi, -1, wszTmp.get(), nNeededLen);
szUnicode = wszTmp.get();
return true;
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时44分
func : Unicode转ANSI
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::UnicodeToAnsi(const wchar_t* szUnicode, string& szAnsi)
{
UINT nNeededLen = WideCharToMultiByte(CP_ACP, 0, szUnicode, -1, NULL, 0, NULL, NULL);
unique_ptr<char[]> szTmp(new char[nNeededLen]);
if(!szTmp)
{
return false;
}
WideCharToMultiByte(CP_ACP, 0, szUnicode, -1, szTmp.get(), nNeededLen, NULL, NULL);
szAnsi = szTmp.get();
return true;
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时44分
func : Unicode转UTF8
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::UnicodeToUTF8(const wchar_t* szUnicode, string& szUTF8)
{
UINT nNeededLen = WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, NULL, 0, NULL, NULL);
unique_ptr<char[]> szTmp(new char[nNeededLen]);
if(!szTmp)
{
return false;
}
WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, szTmp.get(), nNeededLen, NULL, NULL);
szUTF8 = szTmp.get();
return true;
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时45分
func : Ansi转UTF8
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::AnsiToUTF8(const char* szAnsi, string& szUTF8)
{
wstring wstrTmp;
if(!AnsiToUnicode(szAnsi, wstrTmp))
{
return false;
}
if(!UnicodeToUTF8(wstrTmp.c_str(), szUTF8))
{
return false;
}
return true;
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时45分
func : UTF8转Unicode
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::UTF8ToUnicode(const char* szUTF8, wstring& strUnicode)
{
UINT nNeededLen = MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, NULL, 0);
unique_ptr<wchar_t[]> szTmp(new wchar_t[nNeededLen]);
if(!szTmp)
{
return false;
}
MultiByteToWideChar(CP_UTF8, 0, szUTF8, -1, szTmp.get(), nNeededLen);
strUnicode = szTmp.get();
return true;
}
/*-------------------------------------------------------------------------
time : 2015年5月8日 16时45分
func : UTF8转Ansi
para :
ret :
-------------------------------------------------------------------------*/
bool CCharCvt::UTF8ToAnsi(const char* szUTF8, string& strAnsi)
{
wstring strTmp;
if(!UTF8ToUnicode(szUTF8, strTmp))
{
return false;
}
if(!UnicodeToAnsi(strTmp.c_str(), strAnsi))
{
return false;
}
return true;
}