字体转换器c语⾔代码⼤全,c语⾔字符编码转换
使⽤libiconv. 本⽂包括libiconv实⽤简单介绍及⼏个例⼦程序。
libiconv实⽤⾮常简单,只有3步:
⼀个⼩例⼦:
字体代码大全/* lanconv.c - a simple example to convert GB2312 to UTF-8/BIG5 * * Author: zhou_weicheng at 163 dot com * Date:
2005/11/30 */ #include stdio.h #include stdlib.h #include string.h #include iconv.h int main(int argc, char *argv[]) { iconv_t cd; size_t n, inlen, outlen; char buf[1024]; char *in, *out; // cd = iconv_open("UTF-8", "GB2312"); cd = iconv_open("BIG5", "GB2312"); if (cd == (iconv_t)-1) { perror("iconv_open"); exit(-1); } in = argv[1]; out = buf; inlen = strlen(in); outlen =
sizeof(buf); n = iconv(cd, &in, &inlen, &out, &outlen); if (n == -1) { perror("iconv"); exit(-1); } printf("%s ", buf);
iconv_close(cd); exit(0); }
⼀个例⼦from:
#include stdio.h #include string.h #include iconv.h int main(int argc, char **argv) { FILE *fin, *fout; char *encFrom, *encTo; char bufin[1024], bufout[1024], *sin, *sout; int mode, lenin, lenout, ret, nline; iconv_t c_pt; if (argc != 5) { printf("Usage:
a.out "); return 0; } encFrom = argv[1]; encTo = argv[2]; if ((fin = fopen(argv[3], "rt")) == NULL) { printf("Cannot open file: %s ", argv[3]); return -1; } if ((fout = fopen(argv[4], "wt")) == NULL) { printf("Cannot open file: %s ", argv[4]); return -1; } if ((c_pt = iconv_open(encTo, encFrom)) == (iconv_t)-1) { printf("iconv_open false: %s ==> %s ", encFrom, encTo); return -1; } iconv(c_pt, NULL, NULL, NULL, NULL); nline = 0; while (fgets(bufin, 1024, fin) != NULL) { nline ++; lenin = strlen(bufin) + 1; lenout = 1024; sin = bufin; sout = bufout; ret = iconv(c_pt, &sin, &lenin, &sout, &lenout); printf("%s -> %s: %d: ret=%d,
len_in=%d, len_out=%d ", encFrom, encTo, nline, ret, lenin, lenout); if (ret == -1) { printf("stop at: %s ", sin); break; }
fprintf(fout, "%s", bufout); } iconv_close(c_pt); fclose(fin); fclose(fout); return 0; }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论