字符編碼 utf-8 與 gb2312之間的轉換
字符編碼查詢 漢字字符集編碼查詢
設備之間的收發端使用的gb2312, 比如“早”字編碼爲(D4E7), 而網絡下載的txt文件的編碼爲utf-8
爲了比較這兩類文件,需要轉化到統一的編碼,這裏將 utf-8轉化爲gb2312
C 實現
//代碼轉換:從一種編碼轉爲另一種編碼
static int code_convert(char * from_charset, char * to_charset, char * inbuf, size_t inlen, char * outbuf, size_t outlen)
{
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
int ret ;
cd = iconv_open(to_charset, from_charset);
if(cd == (iconv_t)-1)
{
perror("open iconv_open failed");
return -1;
}
memset(outbuf,0,outlen);
ret = iconv(cd, (char **)pin, &inlen, (char **)pout, &outlen);
if( ret < 0)
{
perror("iconv failed");
iconv_close(cd); /*by zyy*/
return -1;
}
iconv_close(cd);
return 0;
}
/*UTF-8 碼轉爲GB2312碼*/
static int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen)
{
int ret;
ret =code_convert("UTF-8","GBK//IGNORE",inbuf,inlen,outbuf,outlen);
return ret;
}