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\n", pFromCharset, pToCharset); return -1; } memset(pOutBuf, 0, nOutLen); if (iconv(cd, pin, &nInLen, pout, &nOutLen) == -1) { printf("iconv err! %s to %s\n", 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!\n"); 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\n", outbuf); gbkToUnicode(inbuf, strlen(inbuf), outbuf, sizeof(outbuf)); printf("gbk to unicode out: %s\n", outbuf); }
相关推荐
88384957 2020-06-12
ILVNMM 2020-10-26
PinkBean 2020-08-19
Seandba 2020-08-16
徐建岗网络管理 2020-07-28
lynjay 2020-06-14
AaronPlay 2020-06-13
herohope 2020-06-10
adwen00 2020-06-09
KilluaZoldyck 2020-06-06
hitxueliang 2020-06-05
lightindark 2020-06-03
baishuwei 2020-06-03
jediaellu 2020-05-31
twater000 2020-05-29
ItBJLan 2020-05-11
韩学敏 2020-05-09