字符编码转换类(支持多国语言)

头文件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));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值