windows API实现中文中字符串与GBK、Unicode、UTF-8三种编码互转

#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

//gbk转UTF-8
string GbkToUtf8(const std::string& strGbk)//传入的strGbk是GBK编码
{
	//gbk转unicode
	int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);
	wchar_t *strUnicode = new wchar_t[len];
	wmemset(strUnicode, 0, len);
	MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);

	//unicode转UTF-8
	len = WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, NULL, 0, NULL, NULL);
	char * strUtf8 = new char[len];
	WideCharToMultiByte(CP_UTF8, 0, strUnicode, -1, strUtf8, len, NULL, NULL);

	std::string strTemp(strUtf8);//此时的strTemp是UTF-8编码
	delete[]strUnicode;
	delete[]strUtf8;
	strUnicode = NULL;
	strUtf8 = NULL;
	return strTemp;
}

//UTF-8转gbk
string Utf8ToGbk(const std::string& strUtf8)//传入的strUtf8是UTF-8编码
{
	//UTF-8转unicode
	int len = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, NULL, 0);
	wchar_t * strUnicode = new wchar_t[len];//len = 2
	wmemset(strUnicode, 0, len);
	MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, strUnicode, len);

	//unicode转gbk
	len = WideCharToMultiByte(CP_ACP, 0, strUnicode, -1, NULL, 0, NULL, NULL);
	char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0
	memset(strGbk, 0, len);
	WideCharToMultiByte(CP_ACP,0, strUnicode, -1, strGbk, len, NULL, NULL);

	std::string strTemp(strGbk);//此时的strTemp是GBK编码
	delete[]strUnicode;
	delete[]strGbk;
	strUnicode = NULL;
	strGbk = NULL;
	return strTemp;
}

//gbk转unicode (下面的例子没用到)
wstring GbkToUnicode(const std::string& strGbk)//返回值是wstring
{
	int len = MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, NULL, 0);
	wchar_t *strUnicode = new wchar_t[len];
	wmemset(strUnicode, 0, len);
	MultiByteToWideChar(CP_ACP, 0, strGbk.c_str(), -1, strUnicode, len);

	std::wstring strTemp(strUnicode);//此时的strTemp是Unicode编码
	delete[]strUnicode;
	strUnicode = NULL;
	return strTemp;
}

//Unicode转gbk
string UnicodeToGbk (const std::wstring& strUnicode)//参数是wstring
{
	int len = WideCharToMultiByte(CP_ACP, 0, strUnicode.c_str(), -1, NULL, 0, NULL, NULL);
	char *strGbk = new char[len];//len=3 本来为2,但是char*后面自动加上了\0
	memset(strGbk, 0, len);
	WideCharToMultiByte(CP_ACP,0,strUnicode.c_str(), -1, strGbk, len, NULL, NULL);

	std::string strTemp(strGbk);//此时的strTemp是GBK编码
	delete[]strGbk;
	strGbk = NULL;
	return strTemp;
}

int main()
{
	//1、ANSI/GBK编码
	string strGbk = "我";
	int num = strGbk.size();//获取两个字符数,也是我字所占的字节数

	unsigned char* p = (unsigned char*)strGbk.c_str();    
	for (int i = 0; i < num; i++)    
	{    
		printf("%0x", *p);    
		p++;    
	}  //输出ced2 所以我的GBK编码是0xced2
	printf("\n");   

	char gbk[] = {0xce, 0xd2, 0x00}; //加上0x00字符串结束符,不会输出乱码
	cout<<gbk<<endl;//输出汉字我


	//2、unicodde编码

	//方法一
	//wchar_t str = 0x6211;  
	//wcout.imbue(locale("chs")); 
	//wcout << str << endl;//输出汉字我

	//wchar_t c=L'我';
	//cout << hex << (short)c << endl<<endl;//输出unicodde编码 6211

	//方法二:
	wstring strUnicode = L"我";//转成unicode编码
	num = strUnicode.size()*2;//乘以2,才是我所占的字节数
	p = (unsigned char*)strUnicode.c_str();    
	for (int i = 0; i < num; i++)    
	{    
		printf("%0x", *p);    
		p++;    
	}  //输出1162 因为默认是小端模式,所以我的unicode编码是0x6211
	printf("\n");   

	wchar_t s[2] = {0x6211, 0x00}; //加上0x00字符串结束符,不会输出乱码
	wstring str =(wchar_t*)s;
	cout<<UnicodeToGbk(str)<<endl;//需要先将unicode字符串转成gbk之后才能用cout输出


	//3、UTF-8编码
	string strUtf8  = GbkToUtf8("我");//转成utf8编码
	num = strUtf8.size();//num=3
	p = (unsigned char*)strUtf8.c_str();    
	for (int i = 0; i < num; i++)    
	{    
		printf("%0x", *p);    
		p++;    
	}  //输出e68891
	printf("\n");   

	char utf8[] = {0xe6, 0x88, 0x91,0x00}; //加上0x00字符串结束符,不会输出乱码
	cout<<Utf8ToGbk(utf8)<<endl;//需要先将utf8字符串转成gbk之后才能用cout输出


	return 0;
}

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值