{"id":705,"date":"2011-02-24T00:12:51","date_gmt":"2011-02-23T16:12:51","guid":{"rendered":"http:\/\/www.awaysoft.com\/taor\/?p=705"},"modified":"2013-12-23T11:07:40","modified_gmt":"2013-12-23T03:07:40","slug":"iconv-h-%e4%bd%bf%e7%94%a8%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/actom.me\/blog\/iconv-h-%e4%bd%bf%e7%94%a8%e7%ac%94%e8%ae%b0.html","title":{"rendered":"iconv.h \u4f7f\u7528\u7b14\u8bb0"},"content":{"rendered":"<p>\u7814\u7a76\u4e86\u4e00\u6bb5\u65f6\u95f4\u7684iconv.h\uff0c\u7ec8\u4e8e\u5728\u67e5\u770b\u8d44\u6599\u548c\u5b9e\u8df5\u4e0b\u641e\u5b9a\u4e86iconv\u8fd9\u4e2a\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u5bf9\u5305\u542b\u9519\u8bef\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u7684\u8f6c\u7801\u3002\u5e38\u89c1\u7684\u60c5\u51b5\u5c31\u662f\uff0c\u67d0\u4e9b\u5b57\u7b26\u4e0d\u5c5e\u4e8eGBK\u5b57\u7b26\u96c6\uff0c\u800c\u5c06\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7531GBK\u8f6c\u5230UTF-8\u7684\u65f6\u5019\u5c31\u4f1a\u51fa\u9519\u3002<br \/>\n<!--more--><\/p>\n<pre class=\"lang:c decode:true\">int\r\nconvert(const char *from, \/*\u8981\u8f6c\u6362\u7684\u5b57\u7b26\u96c6*\/\r\nconst char *to, \/*\u8981\u8f6c\u6362\u5230\u7684\u5b57\u7b26\u96c6*\/\r\nchar* save, \/*\u8f93\u51fa\u5b57\u7b26\u4e32*\/\r\nint savelen, \/*\u8f93\u51fa\u5b57\u7b26\u4e32\u7f13\u5b58\u957f\u5ea6*\/\r\nchar *src, \/*\u6e90\u5b57\u7b26\u4e32*\/\r\nint srclen) \/*\u6e90\u5b57\u7b26\u4e32\u957f\u5ea6*\/\r\n{\r\niconv_t cd;\r\nchar *inbuf = src;\r\nchar *outbuf = save;\r\nsize_t outbufsize = savelen;\r\nint status = 0;\r\nsize_t savesize = 0;\r\nsize_t inbufsize = srclen;\r\nchar* inptr = inbuf;\r\nsize_t insize = inbufsize;\r\nchar* outptr = outbuf;\r\nsize_t outsize = outbufsize;\r\n\r\ncd = iconv_open(to, from);\r\niconv(cd,NULL,NULL,NULL,NULL);\r\nif (inbufsize == 0) {\r\nstatus = -1;\r\ngoto done;\r\n}\r\nwhile (insize &gt; 0) {\r\nsize_t res = iconv(cd,&amp;inptr,&amp;insize,&amp;outptr,&amp;outsize);\r\nif (res == (size_t)(-1)) {\r\nif (errno == EILSEQ) {\r\ninptr ++;\r\nstatus = -3;\r\n} else if (errno == EINVAL) {\r\nbreak;\r\n} else if (errno == E2BIG) {\r\nstatus = -5;\r\ngoto done;\r\n} else {\r\nstatus = -6;\r\ngoto done;\r\n}\r\n}\r\n}\r\nif (outptr != outbuf) {\r\nint outsize = outptr - outbuf;\r\nstrncpy(save+savesize, outbuf, outsize);\r\n}\r\nstatus = strlen(save);\r\ndone:\r\niconv_close(cd);\r\nreturn status;\r\n}<\/pre>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<br \/>\n<code><\/code><\/p>\n<pre class=\"lang:c decode:true\">gchar *convert(gchar *from)\r\n{\r\nchar *result = g_new(char, strlen(from) * 3 + 1);\r\nconvert2(\"GBK\", \"UTF-8\", result, strlen(from) * 3 + 1, from, strlen(from));\r\nif (result != NULL) return result;\r\nelse return \"\";\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>PS:iconv\u53c2\u8003\u8bf4\u660e\uff1a<br \/>\niconv\u51fd\u6570\u65cf\u7684\u5934\u6587\u4ef6\u662ficonv.h\uff0c\u4f7f\u7528\u524d\u9700\u5305\u542b\u4e4b\u3002<\/p>\n<p>#include<\/p>\n<p>iconv\u51fd\u6570\u65cf\u6709\u4e09\u4e2a\u51fd\u6570,\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:c decode:true \">(1)iconv_t iconv_open(const char *tocode,const char *fromcode)\uff1b<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6b64\u51fd\u6570\u8bf4\u660e\u5c06\u8981\u8fdb\u884c\u54ea\u4e24\u79cd\u7f16\u7801\u7684\u8f6c\u6362\uff0ctocode\u662f\u76ee\u6807\u7f16\u7801\uff0cfromcode\u662f\u539f\u7f16\u7801\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u8f6c\u6362\u53e5\u67c4\uff0c\u4f9b\u4ee5\u4e0b\u4e24\u4e2a\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n<pre class=\"lang:c decode:true \">(2)size_t iconv(iconv_t cd\uff0cchar **inbuf\uff0csize_t *inbytesleft\uff0cchar **outbuf\uff0csize_t *outbytesleft)\uff1b<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6b64\u51fd\u6570\u4eceinbuf\u4e2d\u8bfb\u53d6\u5b57\u7b26\uff0c\u8f6c\u6362\u540e\u8f93\u51fa\u5230outbuf\u4e2d\uff0cinbytesleft\u7528\u4ee5\u8bb0\u5f55\u8fd8\u672a\u8f6c\u6362\u7684\u5b57\u7b26\u6570,outbytesleft\u7528\u4ee5\u8bb0\u5f55\u8f93\u51fa\u7f13\u51b2\u7684\u5269\u4f59\u7a7a\u95f4\u3002<\/p>\n<p>\u53c2\u6570cd\u5fc5\u987b\u662f\u7531iconv_open\u51fd\u6570\u521b\u5efa\u7684\u8f6c\u6362\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u5927\u90e8\u5206\u60c5\u5f62\u662finbuf \u4e0d\u4e3aNULL\uff0c*inbuf\u4e5f\u4e0d\u4e3aNULL\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ciconv\u51fd\u6570\u5c06\u4ee5*inbuf\u8d77\u59cb\u7684\u591a\u5b57\u8282\u5e8f\u5217\u8f6c\u6362\u5230\u4ee5*outbuf\u8d77\u59cb\u7684\u591a\u5b57\u8282\u5e8f\u5217\u3002\u4ece*inbuf\u5f00\u59cb\u8bfb\u53d6,\u6700\u591a*inbytesleft\u5b57\u8282\uff0c\u8f6c\u6362\u540e\uff0c\u4ece*outbuf\u5f00\u59cb\u5199\u5165,\u6700\u591a*outbytesleft\u5b57\u8282\u3002<\/p>\n<p>iconv\u51fd\u6570\u4e00\u6b21\u8f6c\u6362\u4e00\u4e2a\u591a\u5b57\u8282\u5b57\u7b26\uff0c\u6bcf\u6b21\u5b57\u7b26\u8f6c\u6362\uff0c*inbuf\u589e\u52a0\u5df2\u8f6c\u6362\u7684\u5b57\u8282\u6570\uff0c*inbytesleft\u76f8\u5e94\u5730\u51cf\u5c11\u5df2\u8f6c\u6362\u7684\u5b57\u8282\u6570\uff1b\u5bf9\u5e94\u5730\uff0c*outbuf\u548c*outbytesleft\u4f5c\u76f8\u5e94\u7684\u4fee\u6539\uff0c\u540c\u65f6\u4fee\u6539cd\u7684\u8f6c\u6362\u72b6\u6001\u3002<\/p>\n<p>\u4ee5\u4e0b\u56db\u79cd\u60c5\u51b5\u4e0d\u80fd\u5b8c\u6210\u8f6c\u6362\uff1a<\/p>\n<p>1\uff0e\u8f93\u5165\u4e2d\u542b\u65e0\u6548\u7684\u591a\u5b57\u8282\u5e8f\u5217\u3002\u6b64\u65f6\uff0c\u5b83\u5c06errno\u8bbe\u7f6e\u4e3aEILSEQ\u5e76\u8fd4\u56de(size_t)(-1)\u3002*inbuf\u6307\u5411\u65e0\u6548\u5e8f\u5217\u7684\u6700\u5de6\u7aef\u3002<\/p>\n<p>2\uff0e\u8f93\u5165\u7684\u5b57\u8282\u5e8f\u5217\u5df2\u7ecf\u5168\u90e8\u88ab\u8f6c\u6362\u8fc7\uff0c\u4e5f\u5c31\u662f*inbytesleft\u51cf\u5c11\u81f30\u3002\u6b64\u65f6\uff0ciconv\u8fd4\u56de\u672c\u6b21\u8c03\u7528\u4e2d\u5b8c\u6210\u8f6c\u6362\u7684\u6570\u76ee\uff08\u53ef\u9006\u7684\u8f6c\u6362\u4e0d\u8ba1\u5165\uff09\u3002<\/p>\n<p>3\uff0e\u8f93\u5165\u4e2d\u4ee5\u4e0d\u5b8c\u6574\u591a\u5b57\u8282\u5e8f\u5217\u4f5c\u7ed3\u5c3e\u3002\u6b64\u65f6\uff0c\u5b83\u5c06errno\u8bbe\u7f6e\u4e3aEINVAL\u5e76\u8fd4\u56de(size_t)(-1)\u3002*inbuf\u6307\u5411\u4e0d\u5b8c\u6574\u591a\u5b57\u8282\u5e8f\u5217\u7684\u6700\u5de6\u7aef\u3002<\/p>\n<p>4\uff0e\u8f93\u51fa\u7f13\u5b58\u533a\u6ca1\u6709\u8db3\u591f\u7a7a\u95f4\u6765\u5b58\u50a8\u4e0b\u4e00\u4e2a\u5b57\u7b26\u3002\u6b64\u65f6\uff0c\u5b83\u5c06errno\u8bbe\u7f6e\u4e3aE2BIG\u5e76\u8fd4\u56de(size_t)(-1)\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u60c5\u5f62\u662finbuf \u4e3aNULL\u6216*inbuf\u4e3aNULL\uff0c\u4f46*outbuf \u4e0d\u4e3aNULL\uff0c*outbuf\u4e5f\u4e0d\u4e3aNULL\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ciconv\u51fd\u6570\u8bd5\u56fe\u5c06cd\u7684\u8f6c\u6362\u72b6\u6001\u8bbe\u7f6e\u4e3a\u521d\u59cb\u72b6\u6001\u5e76store a corresponding shift sequence at *outbuf\u3002\u4ece*outbuf\u5f00\u59cb\uff0c\u6700\u591a\u5199\u5165*outbytesleft\u5b57\u8282\u3002\u5982\u679c\u8f93\u51fa\u7f13\u5b58\u533a\u6ca1\u6709\u8db3\u591f\u7a7a\u95f4\u6765\u5b58\u50a8\u8fd9\u4e2a\u91cd\u7f6e\u540e\u7684\u5e8f\u5217\uff0c\u4ed6\u5c06errno\u8bbe\u7f6e\u4e3aE2BIG\u5e76\u8fd4\u56de(size_t)(-1)\u3002\u53cd\u4e4b\uff0c*outbuf\u589e\u52a0\u5199\u5165\u7684\u5b57\u8282\u6570\u548c*outbytesleft\u51cf\u5c11\u5199\u5165\u7684\u5b57\u8282\u6570\u3002<\/p>\n<p>\u7b2c\u4e09\u79cd\u60c5\u5f62\u662finbuf \u4e3aNULL\u6216*inbuf\u4e3aNULL\uff0c*outbuf \u4e3aNULL\u6216*outbuf\u4e3aNULL\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0ciconv\u51fd\u6570\u8bd5\u56fe\u5c06cd\u7684\u8f6c\u6362\u72b6\u6001\u8bbe\u7f6e\u4e3a\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<p>\u8fd4\u56de\u503c<\/p>\n<p>iconv\u51fd\u6570\u8fd4\u56de\u672c\u6b21\u8c03\u7528\u4e2d\u8f6c\u6362\u7684\u5b57\u7b26\u6570\uff0c\u53ef\u9006\u7684\u8f6c\u6362\u4e0d\u8ba1\u5165\u3002\u51fa\u9519\u65f6\uff0c\u5b83\u5c06\u4fee\u6539errno\u5e76\u8fd4\u56de(size_t)(-1)\u3002<\/p>\n<p>\u9519\u8bef<\/p>\n<p>\u9664\u4e86\u5176\u5b83\u9519\u8bef\u4ee5\u5916\uff0c\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<p>E2BIG<\/p>\n<p>*outbuf\u6ca1\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u3002<\/p>\n<p>EILSEQ<\/p>\n<p>\u8f93\u5165\u542b\u65e0\u6548\u7684\u591a\u5b57\u8282\u5e8f\u5217\u3002<\/p>\n<p>EINVAL<\/p>\n<p>\u8f93\u5165\u542b\u4e0d\u5b8c\u6574\u591a\u5b57\u8282\u5e8f\u5217\u3002<\/p>\n<pre class=\"lang:c decode:true \">(3)int iconv_close(iconv_t cd)\uff1b<\/pre>\n<p>&nbsp;<\/p>\n<p>\u6b64\u51fd\u6570\u7528\u4e8e\u5173\u95ed\u8f6c\u6362\u53e5\u67c4,\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7814\u7a76\u4e86\u4e00\u6bb5\u65f6\u95f4\u7684iconv.h\uff0c\u7ec8\u4e8e\u5728\u67e5\u770b\u8d44\u6599\u548c\u5b9e\u8df5\u4e0b\u641e\u5b9a\u4e86iconv\u8fd9\u4e2a\u51fd\u6570\uff0c &#8230; <a class=\"more-link\" href=\"https:\/\/actom.me\/blog\/iconv-h-%e4%bd%bf%e7%94%a8%e7%ac%94%e8%ae%b0.html\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,4],"tags":[],"_links":{"self":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/705"}],"collection":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":5,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actom.me\/blog\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}