gb2312-unicode(ucs2)

BOOL UCS2_TO_GB2312(std::string &dst, LPCWSTR src)
{
	const UINT CP_GB2312 = 936;
	BOOL IsOk;
	LONG nBytes;

	IsOk = FALSE;
	nBytes = WideCharToMultiByte(CP_GB2312, 0, src, -1, NULL, 0, NULL, NULL);
	if (nBytes > 0)
	{
		HANDLE hHeapHandle = GetProcessHeap();

		if (LPSTR mb = reinterpret_cast<LPSTR>(HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, nBytes)))
		{
			if (WideCharToMultiByte(CP_GB2312, 0, src, -1, mb, nBytes, NULL, NULL) == nBytes)
			{
				dst.append(mb);
				IsOk = TRUE;
			}
			HeapFree(hHeapHandle, 0, mb);
		}
	}
	return IsOk;
}

BOOL GB2312_TO_UCS2(std::wstring &dst, LPCSTR src)
{
	const UINT CP_GB2312 = 936;
	BOOL IsOk;
	LONG nCharacters;

	IsOk = FALSE;
	nCharacters = MultiByteToWideChar(CP_GB2312, 0, src, -1, NULL, 0);
	if (nCharacters > 0)
	{
		HANDLE hHeapHandle = GetProcessHeap();

		if (LPWSTR wc = reinterpret_cast<LPWSTR>(HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, nCharacters * sizeof(wchar_t))))
		{
			if (MultiByteToWideChar(CP_GB2312, 0, src, -1, wc, nCharacters) == nCharacters)
			{
				dst.append(wc);
				IsOk = TRUE;
			}
			HeapFree(hHeapHandle, 0, wc);
		}
	}
	return IsOk;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章