unicode 码点、 unicode utf8编码、GB2312/GBK/GB18030编码的互转的例子

#define testUnicode main
//Refer to https://www.qqxiuzi.cn/bianma/zifuji.php 
//https://blog.csdn.net/hyongilfmmm/article/details/112045049
void testUnicode()
{
	std::wstring a = L"汉"; //code point @ 0x6c49 ,a赋值的是unicode码点
	unsigned char aa[10];            // code point @ 0x6c49 encoded in utf8 is 0xE6 B1 89
	                        // in GB2312/GBK/GB18030 code is 0xBABA 
	WideCharToMultiByte(CP_UTF8, 0, a.c_str(), -1, (LPSTR)aa, 10, NULL, NULL);
	unsigned  char bb[10];
	WideCharToMultiByte(CP_ACP, 0, a.c_str(), -1, (LPSTR)bb, 10, NULL, NULL);
	std::cout <<"unicode code point,(No.) :" <<  std::hex << a.at(0) << std::endl;
	std::cout << "unicode code point, encode using UTF8:" << std::hex << (int)aa[0] <<","<< (int)aa[1]<<","<< (int)aa[2]<<","<< (int)aa[3] << std::endl;
	std::cout << "unicode code point, encode using ACP (GB2312/GBK/GB18030):" << std::hex;
	std::cout << (int)bb[0] << "," << (int)bb[1] << "," << (int)bb[2] << "," << (int)bb[3]  << std::endl;
}

输出:
 

unicode code point,(No.) :6c49
unicode code point, encode using UTF8:e6,b1,89,0
unicode code point, encode using ACP (GB2312/GBK/GB18030):ba,ba,0,cc

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值