libiconv gbk utf-8 转 unicode
Posted zbs337650377
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libiconv gbk utf-8 转 unicode相关的知识,希望对你有一定的参考价值。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "iconv.h" #define CODE_UNICODE "UCS-2LE" int code_convert(const char *pFromCharset, const char *pToCharset, char *pInBuf, size_t nInLen, char *pOutBuf, size_t nOutLen) { iconv_t cd; int rc; /* 防止指针偏移. */ char **pin = &pInBuf; char **pout = &pOutBuf; cd = iconv_open(pToCharset, pFromCharset); if (cd == 0) { printf("iconv_open err! %s to %s ", pFromCharset, pToCharset); return -1; } memset(pOutBuf, 0, nOutLen); if (iconv(cd, pin, &nInLen, pout, &nOutLen) == -1) { printf("iconv err! %s to %s ", pFromCharset, pToCharset); return -2; } iconv_close(cd); return 0; } int codeToConvert(const char *pFromCharset, const char *pToCharset, char *pInBuf, int nInLen, char *pOutBuf, int nOutLen) { char *tmpBuf = (char*)malloc(nOutLen); if (!tmpBuf) { printf("invalid params! "); return -1; } int nRet = code_convert(pFromCharset, pToCharset, pInBuf, nInLen, tmpBuf, nOutLen); if (0 == nRet) { memset(pOutBuf, 0, nOutLen); for (int i = 0; i < nOutLen; ++i) { if (i % 2 == 0 && *(short*)(tmpBuf + i) == 0) break; sprintf(pOutBuf + 2 * i, "%02x", tmpBuf[i] & 0xff); } } free(tmpBuf); tmpBuf = NULL; return nRet; } int utf8ToUnicode(char *pInBuf, int nInLen, char *pOutBuf, int nOutLen) { return codeToConvert("UTF-8", CODE_UNICODE , pInBuf, nInLen, pOutBuf, nOutLen); } int gbkToUnicode(char *pInBuf, int nInLen, char *pOutBuf, int nOutLen) { return codeToConvert("GBK", CODE_UNICODE, pInBuf, nInLen, pOutBuf, nOutLen); } int main() { char inbuf[] = "欢迎你好,hello world!"; char outbuf[128] = { 0 }; utf8ToUnicode(inbuf, strlen(inbuf), outbuf, sizeof(outbuf)); printf("utf-8 to unicode out: %s ", outbuf); gbkToUnicode(inbuf, strlen(inbuf), outbuf, sizeof(outbuf)); printf("gbk to unicode out: %s ", outbuf); }
以上是关于libiconv gbk utf-8 转 unicode的主要内容,如果未能解决你的问题,请参考以下文章