1、如何界定是utf-8编码还是其他如 ANSI 或者gb2312编码
以“浙”这个汉字为例,用16进制编码查看时,显示 D5 E3 为2个字节,则为 ansi或者gb2312编码
"苏" 显示 ffffffe8 ffffff8b ffffff8f 一个汉字3个字节保存,为utf-8编码
gb2312转utf-8编码
#include <iconv.h>
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen) { iconv_t cd; int rc; char **pin = &inbuf; char **pout = &outbuf;
cd = iconv_open(to_charset, from_charset);
if (cd == 0) return 1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1) return 1;
iconv_close(cd); return 0; }
int u2g(char *inbuf,int inlen,char *outbuf,int outlen) { return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen); } /*GB2312码转为UNICODE码*/ int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen) { return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen); }