UTF-8转化ANSI编码

说明:

上篇文章介绍了,由ANSI编码格式转化为UTF-8格式,文章链接:http://blog.csdn.net/hong__fang/article/details/42241825

本文主要介绍:由UTF-8格式转化为ANSI编码格式:

一、UTF-8转化ANSI编码程序:

string Utf82Ansi(const char* srcCode)
{	
	int srcCodeLen=0;
	//计算接收到待转换字符串的缓冲区所需宽字符数
	srcCodeLen=MultiByteToWideChar(CP_UTF8,NULL,srcCode,strlen(srcCode),NULL,0);
	wchar_t* result_t=new wchar_t[srcCodeLen+1];
	//向result_t缓冲区写入宽字符
	MultiByteToWideChar(CP_UTF8,NULL,srcCode,strlen(srcCode),result_t,srcCodeLen);
	result_t[srcCodeLen]='\0';
	//计算接收到待转换字符串的缓冲区所需字节数
	srcCodeLen=WideCharToMultiByte(CP_ACP,NULL,result_t,wcslen(result_t),NULL,0,NULL,NULL);
	char* result=new char[srcCodeLen+1];
	//向result缓冲区写入字符
	WideCharToMultiByte(CP_ACP,NULL,result_t,wcslen(result_t),result,srcCodeLen,NULL,NULL);
	result[srcCodeLen]='\0';
	string srcAnsiCode="";
	srcAnsiCode=(string)result;
	delete result_t;
	delete result;
	return srcAnsiCode;
}

二、调用形式:

string str = "汉字";
string strTemp = Utf82Ansi(str.c_str());

注:这是string类型字符串调用形式,其他类型,可转化为const char* 或 string类型调用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值