Windows下的字符集转换(ASCII、UICODE、UTF8、GB2312和BIG5互转)

我们在使用字符串的时候会遇到各种各样的编码问题不胜其烦。

本例例举了一些windows下常用的字符集转换函数。方便使用

Unicode和Ascii的互转

//unicode转为ascii
std::string UnicodeToAscii( const std::wstring& in_str )
{
	int nNeedChars = WideCharToMultiByte( CP_ACP, 0, in_str.c_str(), -1, 0, 0, 0, 0 );
	if (nNeedChars > 0)//再次判断一下
	{	
		std::string temp;
		temp.resize(nNeedChars);
		::WideCharToMultiByte( CP_ACP, 0, in_str.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
		return temp;
	}

	return std::string();
}

//ascii转为unicode
std::wstring AsciiToUnicode( const std::string& in_str )
{
	int nNeedWchars = MultiByteToWideChar( CP_ACP, 0, in_str.c_str(), -1, NULL, 0 );
	if (nNeedWchars > 0)
	{
		std::wstring temp;
		temp.resize(nNeedWchars);
		::MultiByteToWideChar( CP_ACP, 0, in_str.c_str(), -1, &temp[0], nNeedWchars );
		return temp;
	}

	return std::wstring();
}

 

Utf8和Unicode的互转

//utf8转为unicode
std::wstring UTF8ToUnicode( const std::string& in_utf8Str )
{
	int nNeedWchars = MultiByteToWideChar( CP_UTF8, 0, in_utf8Str.c_str(), -1, NULL, 0 );
	if (nNeedWchars > 0)
	{
		std::wstring temp;
		temp.resize(nNeedWchars);
		::MultiByteToWideChar( CP_UTF8, 0, in_utf8Str.c_str(), -1, &temp[0], nNeedWchars );
		return temp;
	}

	return std::wstring();
}

//unicode转为utf8
std::string UnicodeToUTF8( const std::wstring& in_wStr )
{
	int nNeedChars = WideCharToMultiByte( CP_UTF8, 0, in_wStr.c_str(), -1, 0, 0, 0, 0 );
	if (nNeedChars > 0)//再次判断一下
	{	
		std::string temp;
		temp.resize(nNeedChars);
		::WideCharToMultiByte( CP_UTF8, 0, in_wStr.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
		return temp;
	}

	return std::string();
}

 

Ascii和Utf8的互转


//ascii转为utf8
std::string AsciiToUTF8(const std::string& in_asciiStr)
{
	return UnicodeToUTF8(AsciiToUnicode(in_asciiStr));
}

//utf8转为ascii
std::string UTF8ToAscii(const std::string& in_utf8Str)
{
	return UnicodeToAscii(UTF8ToUnicode(in_utf8Str));
}

GB2312和Unicode的互转


//GB2312 转换成 Unicode:
std::wstring GB2312ToUnicode(const std::string& strGB2312String)
{
	UINT nCodePage = 936; //GB2312
	int nNeedWchars = MultiByteToWideChar(nCodePage, 0, strGB2312String.c_str(), -1, NULL, 0);
	if (nNeedWchars > 0)
	{
		std::wstring temp;
		temp.resize(nNeedWchars);
		::MultiByteToWideChar( nCodePage, 0, strGB2312String.c_str(), -1, &temp[0], nNeedWchars );
		return temp;
	}

	return std::wstring();
}


//Unicode 转换成 GB2312:
std::string UnicodeToGB2312(const std::wstring& strUnicodeString)
{
	UINT nCodePage = 936; //GB2312
	int nNeedChars = WideCharToMultiByte(nCodePage, 0, strUnicodeString.c_str(), -1, NULL, 0, NULL, NULL);
	//再次判断一下
	if (nNeedChars > 0)
	{
		std::string temp;
		temp.resize(nNeedChars);
		::WideCharToMultiByte( nCodePage, 0, strUnicodeString.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
		return temp;
	}

	return std::string();
}

 

BIG5和Unicode的互转


//BIG5 转换成 Unicode:
std::wstring BIG5ToUnicode(const std::string& strBIG5String)
{
	UINT nCodePage = 950; //BIG5
	int nNeedWchars = MultiByteToWideChar(nCodePage, 0, strBIG5String.c_str(), -1, NULL, 0);
	if (nNeedWchars > 0)
	{
		std::wstring temp;
		temp.resize(nNeedWchars);
		::MultiByteToWideChar( nCodePage, 0, strBIG5String.c_str(), -1, &temp[0], nNeedWchars );
		return temp;
	}

	return std::wstring();
}

//Unicode 转换成 BIG5:
std::string UnicodeToBIG5(const std::wstring& strUnicodeString)
{
	UINT nCodePage = 950; //BIG5
	int nNeedChars = WideCharToMultiByte(nCodePage, 0, strUnicodeString.c_str(), -1, NULL, 0, NULL, NULL);
	//再次判断一下
	if (nNeedChars > 0)
	{
		std::string temp;
		temp.resize(nNeedChars);
		::WideCharToMultiByte( nCodePage, 0, strUnicodeString.c_str(), -1, &temp[0], nNeedChars, 0, 0 );
		return temp;
	}

	return std::string();
}

 

GB2312和BIG5的互转

//繁体中文BIG5 转换成 简体中文 GB2312
std::string BIG5ToGB2312(const std::string& strBIG5String)
{
	std::wstring strUnicode = BIG5ToUnicode(strBIG5String);
	std::string strGB2312 = UnicodeToGB2312(strUnicode);
	return strGB2312;
}

//简体中文 GB2312 转换成 繁体中文BIG5
std::string GB2312ToBIG5(const std::string& strGB2321String)
{
	std::wstring strUnicode = GB2312ToUnicode(strGB2321String);
	std::string strBIG5 = UnicodeToBIG5(strUnicode);
	return strBIG5;
}

 

用法:


int _tmain(int argc, _TCHAR* argv[])
{
	std::wstring wstr_unicode = L"你好吗";
	std::string str_ascii = UnicodeToAscii(wstr_unicode);

	std::string str_ascii_a = "你好吗ABC";
	std::wstring str_unicode_a = AsciiToUnicode(str_ascii_a);

	std::string str_utf8 = UnicodeToUTF8(wstr_unicode);
	std::wstring str_unicode_b = UTF8ToUnicode(str_utf8);

	std::string str_utf8_a = AsciiToUTF8(str_ascii_a);
	std::string str_ascii_b = UTF8ToAscii(str_utf8_a);

	std::string str_gb2312 = UnicodeToGB2312(wstr_unicode);
	std::string str_big5 = UnicodeToBIG5(wstr_unicode);
	std::string str_big5_a = GB2312ToBIG5(str_gb2312);
	std::wstring str_unicode_c = BIG5ToUnicode(str_big5_a);
	std::string str_gb2312_a = BIG5ToGB2312(str_big5_a);

	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值