行走

求知

字符串编码之间的转换(GB2312<->UTF8<->Unicode)及URLEncoding .

LPWSTR GB2312ToUnicode(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	WCHAR* pBuf = NULL;
	int nLen = MultiByteToWideChar(CP_ACP, 0, lpszInBuf, -1, pBuf, 0);
	pBuf = new WCHAR[nLen];
	memset(pBuf, 0, sizeof(WCHAR) * nLen);
	MultiByteToWideChar(CP_ACP, 0, lpszInBuf, -1, pBuf, nLen);
	return pBuf;
}

LPSTR UnicodeToGB2312(LPCWSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	CHAR* pBuf = NULL;
	int nLen = WideCharToMultiByte(CP_ACP, 0, lpszInBuf, -1, pBuf, 0, NULL, NULL);
	pBuf = new CHAR[nLen];
	memset(pBuf, 0, sizeof(CHAR) * nLen);
	WideCharToMultiByte(CP_ACP, 0, lpszInBuf, -1, pBuf, nLen, NULL, NULL);
	return pBuf;
}

LPWSTR UTF8ToUnicode(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	WCHAR* pBuf = NULL;
	int nLen = MultiByteToWideChar(CP_UTF8, 0, lpszInBuf, -1, pBuf, 0);
	pBuf = new WCHAR[nLen];
	memset(pBuf, 0, sizeof(WCHAR) * nLen);
	MultiByteToWideChar(CP_UTF8, 0, lpszInBuf, -1, pBuf, nLen);
	return pBuf;
}

LPSTR UnicodeToUTF8(LPCWSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	CHAR* pBuf =NULL;
	int nLen = WideCharToMultiByte(CP_UTF8, 0, lpszInBuf, -1, pBuf, 0, NULL, NULL);
	pBuf = new CHAR[nLen];
	memset(pBuf, 0, sizeof(CHAR) * nLen);
	WideCharToMultiByte(CP_UTF8, 0, lpszInBuf, -1, pBuf, nLen, NULL, NULL);
	return pBuf;
}

LPSTR GB2312ToUTF8(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	LPWSTR lpszTmp = GB2312ToUnicode(lpszInBuf);
	LPSTR lpszBuf = UnicodeToUTF8(lpszTmp);
	delete[] lpszTmp;
	lpszTmp = NULL;
	return lpszBuf;
}

LPSTR UTF8ToGB2312(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	LPWSTR lpszTmp = UTF8ToUnicode(lpszInBuf);
	LPSTR lpszBuf = UnicodeToGB2312(lpszTmp);
	delete[] lpszTmp;
	lpszTmp = NULL;
	return lpszBuf;
}

LPSTR GB2312Encoding(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	const int nLen = strlen(lpszInBuf) * 3 + 1;
	CHAR* pBuf = new CHAR[nLen];
	memset(pBuf, 0, sizeof(CHAR) * nLen);
	CHAR szTmp[4] = {0};
	for(int i = 0; i != strlen(lpszInBuf); ++i)
	{
		if(IsDBCSLeadByte(lpszInBuf[i]))
		{
			sprintf_s(szTmp, _countof(szTmp), "%%%02X", lpszInBuf[i] & 0xFF);
		}
		else
		{
			sprintf_s(szTmp, _countof(szTmp), "%c", lpszInBuf[i]);
		}
		sprintf_s(pBuf + strlen(pBuf), nLen - strlen(pBuf), "%s", szTmp);
	}
	return pBuf;
}

LPSTR UTF8Encoding(LPCSTR lpszInBuf)
{
	if(NULL == lpszInBuf)
	{
		return NULL;
	}
	const int nLen = strlen(lpszInBuf) * 3 + 1;
	CHAR* pBuf = new CHAR[nLen];
	memset(pBuf, 0, sizeof(CHAR) * nLen);
	CHAR szTmp[4] = {0};
	for(int i = 0; i != strlen(lpszInBuf); ++i)
	{
		if(IsDBCSLeadByte(lpszInBuf[i]))
		{
			sprintf_s(szTmp, _countof(szTmp), "%%%02X", lpszInBuf[i] & 0xFF);
		}
		else
		{
			sprintf_s(szTmp, _countof(szTmp), "%c", lpszInBuf[i]);
		}
		sprintf_s(pBuf + strlen(pBuf), nLen - strlen(pBuf), "%s", szTmp);
	}
	return pBuf;
}

// 测试代码
#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>

int _tmain(int argc, _TCHAR* argv[])
{
	CHAR szText[] = "百度Hi";
	CHAR* pBuf = GB2312Encoding(szText);
	MessageBoxA(NULL, pBuf, NULL, 0);
	delete[] pBuf;
	pBuf = NULL;
	return 0;
}

转:http://blog.csdn.net/visualeleven/article/details/8121932
阅读更多
个人分类: MFC C++
上一篇在共享DLL中使用MFC 和 在静态库中使用MFC的区别 .
下一篇VC++ CopyFile、MoveFile函数用法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭