最近研究了一下,vs2008下的汉字字符内码转换。总结了一小下:
国外网站喜欢用UTF8,那是因为英文为主,按照unicode操作的话,英文字母全被存储为两个字节,对于字母系的地区来说,用两个字节存储只有字母太浪费了,不如用UTF8来的更为节省些,因为UTF8格式下的英文字母还是一个字节。
而对于国内网站来说,汉字为主,UTF8格式下每个汉字占用3个字节,远不如unicode格式占用2个字节节省空间。
而在vc2008 下对汉字内码转换简单总结了两段转换代码,效率先不说,至少可以正常工作。
// 汉字转码函数UTF8转Unicode
// 本函数应用在mysql取出汉字的内码转换,所以传入参数稍稍特定了一下
CString CVisitorSystemDlg::UTF8toUnicode(char** UTF8Str,int iNum)
{
int wcharNum; //UTF8汉字内码字符长度
int strNum; //Unicode汉字内码字符长度
wchar_t wcharStr[WCH_LEN]; //WCH_LEN定义为一个固定长度wchar_t字符串空间长度
CString Cstr;
wcharNum = MultiByteToWideChar(CP_UTF8,0,UTF8Str[iNum],WCH_LEN,wcharStr,0);
//第一次运行获取长度
MultiByteToWideChar(CP_UTF8,0,UTF8Str[iNum],WCH_LEN,wcharStr,wcharNum);
//第二次转换UTF8汉字为wchar_t类型字串
strNum = WideCharToMultiByte (CP_ACP,0,wcharStr,wcharNum,Cstr.GetBuffer(),0,NULL,NULL);
//依然获取待转换长度
WideCharToMultiByte(CP_ACP,0,wcharStr,wcharNum,Cstr.GetBuffer(strNum),strNum,NULL,NULL);
//将wchar_t类型汉字字符串转换为CString类型字符串
return Cstr;
}
// Unicode内码汉字转为UTF8字符
char* CVisitorSystemDlg::UnicodeToUTF8(CString cstr)
{
int u8Len = 0; //待转换字符串长度
wchar_t wch[256];
MultiByteToWideChar( CP_ACP, 0, cstr, -1, wch, 256);
//CString unicode汉字字符串转为wchar_t类型字符串
u8Len = WideCharToMultiByte( CP_UTF8, NULL, wch, wcslen(wch), NULL, 0, NULL, NULL);
//第一次运行,wchar_t字符串转换为UTF8内码字符串所需字符长度
char* szU8 = new char[u8Len + 1];
//申请了动态char字符串空间
WideCharToMultiByte(CP_UTF8, NULL, wch, wcslen(wch), szU8, u8Len, NULL, NULL);
//第二次由unicode转换为UTF8内码汉字字符串
szU8[u8Len] = '/0'; //char字符串结尾,本例是mysql查询语句使用
return szU8;
}