兼容VC与GCC的 宽字符 – 多字节字符 转换

std::wstring MBytesToWString(const char* lpcszString)
{
	if (lpcszString == NULL)
	{
		return L"";
	}

#ifdef _MSC_VER
	int len = strlen(lpcszString);
	int unicodeLen = ::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, NULL, 0);
	wchar_t* pUnicode = new wchar_t[unicodeLen + 1];
	memset(pUnicode, 0, (unicodeLen + 1) * sizeof(wchar_t));
	::MultiByteToWideChar(CP_ACP, 0, lpcszString, -1, (LPWSTR)pUnicode, unicodeLen);
	std::wstring wString = (wchar_t*)pUnicode;
	delete[] pUnicode;
	return wString;
#elif __GNUC__
	return boost::locale::conv::utf_to_utf<wchar_t>(lpcszString);
#endif
}

std::string WStringToMBytes(const wchar_t* lpwcszWString)
{
	if (lpwcszWString == NULL)
	{
		return "";
	}

#ifdef _MSC_VER
	char* pElementText;
	int iTextLen;
	// wide char to multi char
	iTextLen = ::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, NULL, 0, NULL, NULL);
	pElementText = new char[iTextLen + 1];
	memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
	::WideCharToMultiByte(CP_ACP, 0, lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);
	std::string strReturn(pElementText);
	delete[] pElementText;
	return strReturn;
#elif __GNUC__
	return boost::locale::conv::utf_to_utf<char>(lpwcszWString);
#endif
}

std::string WStringToUTF8(const wchar_t* lpwcszWString)
{
#ifdef _MSC_VER
	char* pElementText;
	int iTextLen = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, NULL, 0, NULL, NULL);
	pElementText = new char[iTextLen + 1];
	memset((void*)pElementText, 0, (iTextLen + 1) * sizeof(char));
	::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)lpwcszWString, -1, pElementText, iTextLen, NULL, NULL);
	std::string strReturn(pElementText);
	delete[] pElementText;
	return strReturn;
#elif __GNUC__
	return WStringToMBytes(lpwcszWString);
#endif
}

For VC:

std::string ws2s(const std::wstring& s)
{
	int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)s.c_str(), -1, NULL, 0, NULL, NULL);

	if (nLen <= 0) {
		return std::string("");
	}

	char* pszDst = new char[nLen];
	if (NULL == pszDst) {
		return std::string("");
	}

	WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)s.c_str(), -1, pszDst, nLen, NULL, NULL);
	pszDst[nLen - 1] = 0;

	std::string strTemp(pszDst);
	delete[] pszDst;

	return strTemp;
}
std::wstring s2ws(const std::string& s)
{
	int nLen = s.size();

	int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)s.c_str(), nLen, 0, 0);
	if (nSize <= 0) {
		return std::wstring(L"");
	}

	WCHAR* pwszDst = new WCHAR[nSize + 1];
	if (NULL == pwszDst) {
		return std::wstring(L"");
	}

	MultiByteToWideChar(CP_ACP, 0, (LPCSTR)s.c_str(), nLen, pwszDst, nSize);
	pwszDst[nSize] = 0;

	if (pwszDst[0] == 0xFEFF) // skip Oxfeff
		for (int i = 0; i < nSize; i++)
			pwszDst[i] = pwszDst[i + 1];

	wstring wcharString(pwszDst);
	delete[] pwszDst;

	return wcharString;
}
std::string wstringToUtf8(const std::wstring& str)
{
	std::wstring_convert > strCnv;
	return strCnv.to_bytes(str);
}
std::wstring utf8ToWstring(const std::string& str)
{
	std::wstring_convert< std::codecvt_utf8 > strCnv;
	return strCnv.from_bytes(str);
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

20 − 15 =