GB2312, BIG5, UTF8, Unicode之间的互换

本文探讨了GB2312、BIG5、UTF8和Unicode编码之间的互换操作。重点指出在进行编码转换时,UTF8和GB2312都以char*形式表示,转换后的cha*指针需要由调用者负责释放。同时,提供了编码理解的相关链接以供深入学习。
摘要由CSDN通过智能技术生成
char* UTF8ToGB2312(const char* pStrUTF8)
{
	// 先转成宽字符
	int nStrLen = MultiByteToWideChar(CP_UTF8, 0, pStrUTF8, -1, NULL, 0);
	wchar_t* pWStr = new wchar_t[nStrLen + 1];
	memset(pWStr, 0, nStrLen + 1);
	MultiByteToWideChar(CP_UTF8, 0, pStrUTF8, -1, pWStr, nStrLen);

	// 再转成GB2312
	nStrLen = WideCharToMultiByte(CP_ACP, 0, pWStr, -1, NULL, 0, NULL, NULL);
	char* pStr = new char[nStrLen + 1];
	memset(pStr, 0, nStrLen + 1);
	WideCharToMultiByte(CP_ACP, 0, pWStr, -1, pStr, nStrLen, NULL, NULL);
	if(pWStr)
	{
		delete[] pWStr;
	}
	return pStr;
}


char* GB2312ToUTF8(const char* pStrGB2312)
{
	// 先转成宽字符
	int nStrLen = MultiByteToWideChar(CP_ACP, 0, pStrGB2312, -1, NULL, 0);
	wchar_t* pWStr = new wchar_t[nStrLen + 1];
	memset(pWStr, 0, nStrLen + 1);
	MultiByteToWideChar(CP_ACP, 0, pStrGB2312, -1, pWStr, nStrLen);

	// 再转为UTF8
	nStrLen = WideCharToMultiByte(CP_UTF8, 0, pWStr, -1, NULL, 0, NULL, NULL);
	char* pStr
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值