Linux下GB2312与UTF8相互转换直接有接口调用的,调用的接口iconv_open
- #include <iconv.h>
- iconv_t iconv_open (const char* tocode, const char* fromcode);
下面看下测试程序:
- #include <iostream>
- #include <string>
- #include <errno.h>
- #include <iconv.h>
- //iInLen的长度不包括\0,应该用strlen。返回值是处理后的sOut长度
- static int Utf8ToGb2312(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)
- {
- char *pIn = (char *)sIn;
- char *pOut = sOut;
- size_t ret;
- size_t iLeftLen=iMaxOutLen;
- iconv_t cd;
- cd = iconv_open("gb2312", "utf-8");
- if (cd == (iconv_t) - 1)
- {
- return -1;
- }
- size_t iSrcLen=iInLen;
- ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);
- if (ret == (size_t) - 1)
- {
- iconv_close(cd);
- return -1;
- }
- iconv_close(cd);
- return (iMaxOutLen - iLeftLen);
- }
- //iInLen的长度不包括\0,应该用strlen。返回值是处理后的sOut长度
- static int Gb2312ToUtf8(char *sOut, int iMaxOutLen, const char *sIn, int iInLen)
- {
- char *pIn = (char *)sIn;
- char *pOut = sOut;
- size_t ret;
- size_t iLeftLen=iMaxOutLen;
- iconv_t cd;
- cd = iconv_open("utf-8", "gb2312");
- if (cd == (iconv_t) - 1)
- {
- return -1;
- }
- size_t iSrcLen=iInLen;
- ret = iconv(cd, &pIn,&iSrcLen, &pOut,&iLeftLen);
- if (ret == (size_t) - 1)
- {
- iconv_close(cd);
- return -1;
- }
- iconv_close(cd);
- return (iMaxOutLen - iLeftLen);
- }
- int main(void)
- {
- char* pszOri = "中文字符测试";
- printf("strlen:%d\n", strlen(pszOri));
- char pszDst[50] = {0};
- int iLen = Gb2312ToUtf8(pszDst, 50, pszOri, strlen(pszOri)); // Gb2312ToUtf8
- printf("iLen:%d,pszDst=%s\n", iLen, pszDst);
- printf("------------------!\n");
- char pszGbDst[50] = {0};
- int iNewLen = Utf8ToGb2312(pszGbDst, 50, pszDst, iLen); // Utf8ToGb2312
- printf("iNewLen:%d,pszGbDst=%s\n", iNewLen, pszGbDst);
- return 0;
- }
输出结果:
- strlen:12
- 18,涓枃瀛楃娴嬭瘯
- -----------
- 12,中文字符测试
可以看出,UTF8格式下,一个中文字符占三个字节;而GB2312下占两个字节。
注意:在开发板上转换时记得要带上iconv库.....