linux c 的编码转换

把utf-8转换成gbk,
utf-8是用变长的字节表示汉字,gbk是用两个字节表示汉字(区位码表示)

应用:把utf-8转换成gbk,并取出区位码

#include
#include
#include
#include
#define MAXLEN 32000

int main(){


char * str="中";

char outstr[255];
char * out=outstr;

iconv_t cd;
cd=iconv_open("gbk","utf8");

int len,outlen;
len=strlen(str);
outlen=255;
printf("str len is :%d\n",len);
printf("outlen is %d\n",outlen);

if(cd==(iconv_t)-1){

printf("cd ==-1");

exit(-1);
}

printf("1\n");


size_t iconvre=iconv(cd,
( char **)&str , (size_t *)&len,
(char **) &out , (size_t *)&outlen );

if(iconvre==(size_t)-1){

iconv_close(cd);
printf("iconv ==-1");
exit(0);

}
printf("2\n");
iconv_close(cd);
printf("outstr is :%s\n",outstr);
printf("outstr len is:%d\n",strlen(outstr));
int i;
for( i=0;i<2;i+=2){
int pm=(unsigned char )outstr[i];
int wm=(unsigned char )outstr[i+1];
printf("pm is %d\n",pm);
printf("wm is %d\n",wm);

}


}

你可能感兴趣的:(linux,c)