MFC::字符转码

字符转码

1.01 UTF8ToUnicode

std::wstring UTF8ToUnicode(const std::string& utf8string)
{
	int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
	if (widesize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	
	if (widesize == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<wchar_t> resultstring(widesize);
	int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);
	if (convresult != widesize)
	{
		throw std::exception("La falla!");
	}
	return std::wstring(&resultstring[0]);
}

1.02 UTF8ToASCII

std::string UTF8ToASCII(std::string& strUtf8Code)
{
	std::string strRet("");
	//先把 utf8 转为 unicode   
	std::wstring wstr = UTF8ToUnicode(strUtf8Code);
	//最后把 unicode 转为 ascii   
	strRet = WideByteToAcsi(wstr);
	return strRet;
}
std::string WideByteToAcsi(std::wstring& wstrcode)
{
	int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, NULL, 0, NULL, NULL);
	if (asciisize == ERROR_NO_UNICODE_TRANSLATION)
	{
		throw std::exception("Invalid UTF-8 sequence.");
	}
	if (asciisize == 0)
	{
		throw std::exception("Error in conversion.");
	}
	std::vector<char> resultstring(asciisize);
	int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wstrcode.c_str(), -1, &resultstring[0], asciisize, NULL, NULL);
	if (convresult != asciisize)
	{
		throw std::exception("La falla!");
	}
	return std::string(&resultstring[0]);
}

1.03 ASCIIToUTF8

std::string ASCIIToUTF8(std::string& strASCIICode)
{
	std::string strOutUTF8 = "";
	WCHAR * str1;
	int n = MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, NULL, 0);
	str1 = new WCHAR[n];
	MultiByteToWideChar(CP_ACP, 0, strASCIICode.c_str(), -1, str1, n);
	n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
	char * str2 = new char[n];
	WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
	strOutUTF8 = str2;
	delete[]str1;
	str1 = NULL;
	delete[]str2;
	str2 = NULL;
	return strOutUTF8;
}

2.01 UTF8ToASCII

/* 为了减少字符串的拷贝,改为使用shared_array,性能升级 */
#include <boost/shared_array.hpp>
#define CHAR_EMPTY_ARR_PTR boost::shared_array<char>(new char[1]{'\0'})
#define WCHAR_EMPTY_ARR_PTR boost::shared_array<wchar_t>(new wchar_t[1]{L'\0'})
boost::shared_array<char> UTF8ToASCII(std::string& strUtf8Code)
{
	return std::move(UTF8ToASCII(strUtf8Code.c_str()));
}
boost::shared_array<char> UTF8ToASCII(const char* ch, int nLen /*= -1*/)
{
	 //先把 utf8 转为 unicode
	 auto wstr = UTF8ToUnicode(ch, nLen);
	 //最后把 unicode 转为 ascii
	 return std::move(WideByteToAcsi(wstr.get()));
}

2.02 ASCIIToUTF8

boost::shared_array<char> ASCIIToUTF8(std::string& strASCIICode)
{
	return std::move(ASCIIToUTF8(strASCIICode.c_str()));
}
boost::shared_array<char> ASCIIToUTF8(const char* ch, int nLen/* = -1*/)
{
	 int n = MultiByteToWideChar(CP_ACP, 0, ch, nLen, NULL, 0);
	 if (0 == n)
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	boost::shared_array<wchar_t> ptrWtArr(new wchar_t[n]{ L'\0' });
	if (0 == MultiByteToWideChar(CP_ACP, 0, ch, nLen, ptrWtArr.get(), n))
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	n = WideCharToMultiByte(CP_UTF8, 0, ptrWtArr.get(), -1, NULL, 0, NULL, NULL);
	if (0 == n)
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	boost::shared_array<char> ptrArr(new char[n]{ '\0' });
	if (0 == WideCharToMultiByte(CP_UTF8, 0, ptrWtArr.get(), -1, ptrArr.get(), n, NULL, NULL))
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	return std::move(ptrArr);
}

2.03 UTF8ToUnicode

boost::shared_array<wchar_t> UTF8ToUnicode(const char* ch, int nLen /*= -1*/)
{
	int widesize = ::MultiByteToWideChar(CP_UTF8, 0, ch, nLen, NULL, 0);
	if (0 == widesize)
	{
		return WCHAR_EMPTY_ARR_PTR;
	}
	
	boost::shared_array<wchar_t> ptrWtArr(new wchar_t[widesize]{ L'\0' });
	int convresult = ::MultiByteToWideChar(CP_UTF8, 0, ch, nLen, ptrWtArr.get(), widesize);
	if (0 == convresult)
	{
		return WCHAR_EMPTY_ARR_PTR;
	}
	return std::move(ptrWtArr);
}
boost::shared_array<char> DPC::WideByteToAcsi(std::wstring& wstrcode)
{
	return std::move(WideByteToAcsi(wstrcode.c_str()));
}
boost::shared_array<char> WideByteToAcsi(const wchar_t* wch, int nLen /*= -1*/)
{
	int asciisize = ::WideCharToMultiByte(CP_OEMCP, 0, wch, nLen, NULL, 0, NULL, NULL);
	if (0 == asciisize)
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	boost::shared_array<char> ptrArr(new char[asciisize + 1]{'\0'});
	int convresult = ::WideCharToMultiByte(CP_OEMCP, 0, wch, nLen, ptrArr.get(), asciisize, NULL, NULL);
	if (0 == convresult)
	{
		return CHAR_EMPTY_ARR_PTR;
	}
	return std::move(ptrArr);
}
posted @ 2018-06-21 11:06  osbreak  阅读(483)  评论(0编辑  收藏  举报