C++字符编码转化代码
C++字符编码转换代码,可以自己编译一下看看。
bool GB 2312 _ AnsiToUnicode(char * lpszOut,const char* lpszIn,
const char * lpszCurCode/* = NULL */,
const char * lpszOutCode/* = NULL */)
{
# ifdef WIN32/_ WIN32
//什么都不做
int lpszOut _ Size = GB 2312 _ AnsiToUnicode _ Size(lpsz
//该功能与操作系统有关。在XP下使用CP_THREAD_ACP,在win2000下使用CP _ ACP
多字节TowedChar (CP _ thread _ ACP,0,lpszin,strlen (lpszin)+1,(无符号short *) lps。
# else//_ WIN32
# ifdef HAVE _ ICONV
/*使用ICONV将消息转换为utf-16。
* \"char\ "和\"\ "是本地8位编码*/
iconv_t cd的别名;
//ICONV _ CONST char * from _ str = message;
char * from _ str =(char *)LPS Zin;
char * to _ str = LPS zout;
size _ t from _ SZ = strlen(lpszIn)+1;
//size _ t to _ SZ = from _ SZ * 4;
Size _ t to _ SZ = GB 2312 _ AnsiToUnicode _ Size(lpszIn);
size _ t RES;
int I;
char lpsz codename _ From[30],lpsz codename _ To[30];
if(lpszCurCode)
strcpy(lpszCodeName _ From,lpszCurCode);
else
strcpy(lpszCodeName _ From,\ " GB 2312 \ ");
if(lpszOutCode)
strcpy(lpszCodeName _ To,lpszOutCode);
else
strcpy(lpszCodeName _ To," UTF-16 \ ");
# ifdef _ DEBUG
fprintf(stdout,\ "将' %s '从%s转换为%s\n\ ",lpszIn,lpszCodeName_From,lpszCodeName _ To);
# endif
//if((CD = iconv _ open(\ " unicode \ ",\ " GB 2312 \ ")=(iconv _ t)-1){
if((CD = iconv _ open(lpszCodeName _ To,lpszCodeName _ From))=(iconv _ t)-1){
perror(\ "无法打开iconv \ ");
返回false
}
res = iconv(cd,&from_str,&from_sz,&to_str,& to _ SZ);
if(RES = =-1){
perror(\ "无法使用iconv \ ");
返回false
}
iconv _ close(CD);
# endif
# ifdef _ DEBUG
fprintf(stdout," Convert result: '%s'\n\ ",lpszOut);
# endif
# endif//End of _ WIN32
返回true
}
int GB 2312 _ AnsiToUnicode _ Size(const char * LPS Zin)
{
int I counter = 0;
for(int index = 0;指数;index++){
if((unsigned char)LPS Zin[index]> 127)
{
index++;
}
else
{
;
}
I counter+= 2;
}
I counter+= 4;
# ifdef _ DEBUG
fprintf(stdout,\ " GB 2312 _ AnsiToUnicode _ Size()return % d \ \ n \ ",I counter);
# endif
return I counter;
//return 48;
}
0条评论