C++中GB2312、UTF-8、unicode 之间转换

包含头文件 #include <windows.h>

//GB2312到UTF-8的转换
static int GB2312ToUtf8(const char* gb2312, char* utf8)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return len;
}


//UTF-8到GB2312的转换
static int Utf8ToGB2312(const char* utf8, char* gb2312)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_ACP, 0, wstr, -1, gb2312, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return len;
}

--------------------------------------------------------------------------------------------------------------------------------------

//GB2312到Unicode的转换
static int GB2312ToUnicode(const char* gb2312, char* unicode)
{
	UINT nCodePage = 936; //GB2312
	int len = MultiByteToWideChar(nCodePage, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(nCodePage, 0, gb2312, -1, wstr, len);
	len = len*sizeof(wchar_t);
	memcpy(unicode, wstr, len);
	if(wstr) delete[] wstr;
	return len;
}

//Unicode到GB2312的转换
static int UnicodeToGB2312(const char* unicode, int size, char*gb2312)
{
	UINT nCodePage = 936; //GB2312
	wchar_t* wstr = new wchar_t[size/2+1];
	memcpy(wstr, unicode, size);
	int len = WideCharToMultiByte(nCodePage, 0, wstr, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(nCodePage, 0, wstr, -1, gb2312, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return len;
}
------------------------------------------------------------------------------------------------------
//UTF-8到Unicode的转换
static int Utf8ToUnicode(const char* utf8, char*unicode)
{
	int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len+1];
	memset(wstr, 0, len+1);
	MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
	memcpy(unicode, wstr, len);
	if(wstr) delete[] wstr;
	return len;
}

//Unicode到UTF-8的转换
static int UnicodeToUtf8(const char* unicode, int size, char* utf8)
{
	wchar_t* wstr = new wchar_t[size/2+1];
	memcpy(wstr, unicode, size);
	int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, len, NULL, NULL);
	if(wstr) delete[] wstr;
	return len;
}



  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Windows 平台下,可以使用 MultiByteToWideChar 和 WideCharToMultiByte 函数来完成 GB18030/UTF-8Unicode 编码之间转换。 GB18030/UTF-8 Unicode: ``` std::wstring utf8_to_wstring(const std::string& str) { int length = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0); wchar_t* buffer = new wchar_t[length]; MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, buffer, length); std::wstring result(buffer); delete[] buffer; return result; } std::wstring gb_to_wstring(const std::string& str) { int length = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0); wchar_t* buffer = new wchar_t[length]; MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, buffer, length); std::wstring result(buffer); delete[] buffer; return result; } ``` Unicode GB18030/UTF-8: ``` std::string wstring_to_utf8(const std::wstring& str) { int length = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL); char* buffer = new char[length]; WideCharToMultiByte(CP_UTF8, 0, str.c_str(), -1, buffer, length, NULL, NULL); std::string result(buffer); delete[] buffer; return result; } std::string wstring_to_gb(const std::wstring& str) { int length = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL); char* buffer = new char[length]; WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, buffer, length, NULL, NULL); std::string result(buffer); delete[] buffer; return result; } ``` 需要注意的是,GB18030/UTF-8 Unicode 时,使用的是 CP_UTF8 编码;Unicode GB18030/UTF-8 时,使用的是 CP_ACP 编码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值