头文件StrConvertor.h
/*
* 字符编码转换库,支持多国语言。
*/
#pragma once
#include <string>
class CStrConvertor
{
public:
CStrConvertor();
~CStrConvertor();
std::string UTF8toANSI(const char* lpUTF8);
std::string UTF8toANSI(const std::string& lpUTF8);
std::wstring UTF8toUNICODE(const char* lpUTF8);
std::wstring UTF8toUNICODE(const std::string& lpUTF8);
std::string ANSItoUTF8(const char* lpANSI);
std::string ANSItoUTF8(const std::string& lpANSI);
std::wstring ANSItoUNICODE(const char* lpANSI);
std::wstring ANSItoUNICODE(const std::string& lpANSI);
std::string UNICODEtoANSI(const wchar_t* lpUNICODE);
std::string UNICODEtoANSI(const std::wstring& lpUNICODE);
std::string UNICODEtoUTF8(const wchar_t* lpUNICODE);
std::string UNICODEtoUTF8(const std::wstring& lpUNICODE);
};
源文件StrConvertor.cpp
#include "StrConvertor.h"
#include <windows.h>
CStrConvertor::CStrConvertor()
{
}
CStrConvertor::~CStrConvertor()
{
}
std::wstring CStrConvertor::ANSItoUNICODE(const char* lpANSI)
{
std::wstring szResult = L"";
int len = strlen(lpANSI);
int unicode_len = 0;
unicode_len = MultiByteToWideChar(GetACP(), 0, lpANSI, -1, NULL, 0);
wchar_t* pUnicode = new wchar_t[unicode_len + 1];
memset(pUnicode, NULL, (unicode_len + 1)*sizeof(wchar_t));
MultiByteToWideChar(GetACP(), 0, lpANSI, -1, pUnicode, unicode_len);
szResult = pUnicode;
delete[] pUnicode;
return szResult;
}
std::wstring CStrConvertor::ANSItoUNICODE(const std::string& lpANSI)
{
return ANSItoUNICODE(lpANSI.c_str());
}
std::string CStrConvertor::UNICODEtoUTF8(const wchar_t* lpUNICODE)
{
std::string szResult = "";
int len = wcslen(lpUNICODE);
int utf8_len = 0;
utf8_len = WideCharToMultiByte(CP_UTF8, 0, lpUNICODE, -1, NULL, 0, NULL, NULL);
char* pUTF8 = new char[utf8_len + 1];
memset(pUTF8, NULL, utf8_len + 1);
WideCharToMultiByte(CP_UTF8, 0, lpUNICODE, -1, pUTF8, utf8_len, NULL, NULL);
szResult = pUTF8;
delete[] pUTF8;
return szResult;
}
std::string CStrConvertor::UNICODEtoUTF8(const std::wstring& lpUNICODE)
{
return UNICODEtoUTF8((wchar_t*)lpUNICODE.c_str());
}
std::string CStrConvertor::ANSItoUTF8(const char* lpANSI)
{
return UNICODEtoUTF8(ANSItoUNICODE(lpANSI));
}
std::string CStrConvertor::ANSItoUTF8(const std::string& lpANSI)
{
return UNICODEtoUTF8(ANSItoUNICODE(lpANSI));
}
std::wstring CStrConvertor::UTF8toUNICODE(const char* lpUTF8)
{
std::wstring szResult = L"";
int len = strlen(lpUTF8);
int unicode_len = 0;
unicode_len = MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, NULL, 0);
wchar_t* pUnicode = new wchar_t[unicode_len + 1];
memset(pUnicode, NULL, (unicode_len + 1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0, lpUTF8, -1, pUnicode, unicode_len);
szResult = pUnicode;
delete[] pUnicode;
return szResult;
}
std::wstring CStrConvertor::UTF8toUNICODE(const std::string& lpUTF8)
{
return UTF8toUNICODE(lpUTF8.c_str());
}
std::string CStrConvertor::UNICODEtoANSI(const wchar_t* lpUNICODE)
{
std::string szResult = "";
int len = wcslen(lpUNICODE);
int ansi_len = 0;
ansi_len = WideCharToMultiByte(GetACP(), 0, lpUNICODE, -1, NULL, 0, NULL, NULL);
char* pAnsi = new char[ansi_len + 1];
memset(pAnsi, NULL, ansi_len + 1);
WideCharToMultiByte(GetACP(), 0, lpUNICODE, -1, pAnsi, ansi_len, NULL, NULL);
szResult = pAnsi;
delete[] pAnsi;
return szResult;
}
std::string CStrConvertor::UNICODEtoANSI(const std::wstring& lpUNICODE)
{
return UNICODEtoANSI(lpUNICODE.c_str());
}
std::string CStrConvertor::UTF8toANSI(const char* lpUTF8)
{
return UNICODEtoANSI(UTF8toUNICODE(lpUTF8));
}
std::string CStrConvertor::UTF8toANSI(const std::string& lpUTF8)
{
return UNICODEtoANSI(UTF8toUNICODE(lpUTF8));
}