说明:
上篇文章介绍了,由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类型调用。