关闭

gb2312-unicode(ucs2)

330人阅读 评论(0) 收藏 举报
分类:
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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:273884次
    • 积分:4128
    • 等级:
    • 排名:第7469名
    • 原创:130篇
    • 转载:23篇
    • 译文:0篇
    • 评论:90条
    文章分类
    最新评论