依赖库libiconv,libiconv库的交叉编译不做描述,网上很多
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stdint.h> 4 #include "iconv.h" 5 #include "eventlist.h" 6 7 static int ChangeCode( const char* pFromCode, const char* pToCode, const char* pInBuf, 8 size_t* piInLen, char* pOutBuf, size_t* piOutLen ) 9 { 10 int iRet; 11 char **pin = &pInBuf; 12 char **pout = &pOutBuf; 13 iconv_t hIconv; 14 15 //printf("%s: outlen=%d, inlen=%d\n", __FUNCTION__, *piOutLen, *piInLen); 16 17 //打开字符集转换 18 hIconv = iconv_open( pToCode, pFromCode ); 19 if ( -1 == (int)hIconv ) 20 { 21 perror("iconv_open"); 22 return -1; 23 } 24 //开始转换 25 printf("%s: 1 outlen=%d\n", __FUNCTION__, *piOutLen); 26 iRet = iconv( hIconv, pin, piInLen, pout, piOutLen ); 27 if ( -1 == iRet ) 28 { 29 perror("iconv"); 30 return -1; 31 } 32 printf("%s: 2 outlen=%d\n", __FUNCTION__, *piOutLen); 33 34 //关闭字符集转换 35 iconv_close( hIconv ); 36 37 **pout = '\0'; 38 return iRet; 39 } 40 41 static int MAIN_UTFToGb2312(char * pcFrom, char * pcTo, int iMaxToLen) 42 { 43 char * psInBuf = NULL; 44 char * psOutBuf = NULL; 45 unsigned int iInLen = 0; 46 unsigned int iOutLen = 0; 47 int iRet; 48 49 iInLen = strlen(pcFrom)+1; 50 psInBuf = (char *)malloc(iInLen); 51 if ( NULL == psInBuf ) 52 { 53 return 0; 54 } 55 memset(psInBuf, 0x0, iInLen); 56 memcpy(psInBuf, pcFrom, iInLen); 57 58 iOutLen = iMaxToLen; 59 psOutBuf = (char *)malloc(iOutLen); 60 if ( NULL == psOutBuf ) 61 { 62 free(psInBuf); 63 psInBuf = NULL; 64 return 0; 65 } 66 memset(psOutBuf, 0x0, iOutLen); 67 68 iRet = ChangeCode( "utf-8", "gb2312", psInBuf, &iInLen, psOutBuf, &iOutLen ); 69 //iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen); 70 if ( 0 != iRet ) 71 { 72 printf("ChangeCode: Error\n"); 73 //return 0; 74 } 75 memcpy(pcTo, psOutBuf, iOutLen); 76 printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); 77 78 free(psInBuf); 79 psInBuf = NULL; 80 free(psOutBuf); 81 psOutBuf = NULL; 82 83 return iOutLen; 84 85 } 86 87 static int MAIN_GB2312ToUTF(char * pcFrom, char * pcTo, int iMaxToLen) 88 { 89 char * psInBuf = NULL; 90 char * psOutBuf = NULL; 91 unsigned int iInLen = 0; 92 unsigned int iOutLen = 0; 93 int iRet; 94 95 iInLen = strlen(pcFrom)+1; 96 psInBuf = (char *)malloc(iInLen); 97 if ( NULL == psInBuf ) 98 { 99 return 0; 100 } 101 memset(psInBuf, 0x0, iInLen); 102 memcpy(psInBuf, pcFrom, iInLen); 103 104 iOutLen = iMaxToLen; 105 psOutBuf = (char *)malloc(iOutLen); 106 if ( NULL == psOutBuf ) 107 { 108 free(psInBuf); 109 psInBuf = NULL; 110 return 0; 111 } 112 memset(psOutBuf, 0x0, iOutLen); 113 114 iRet = ChangeCode( "gb2312", "utf-8", psInBuf, &iInLen, psOutBuf, &iOutLen ); 115 //iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen); 116 if ( 0 != iRet ) 117 { 118 printf("ChangeCode: Error\n"); 119 //return 0; 120 } 121 memcpy(pcTo, psOutBuf, iOutLen); 122 printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); 123 124 free(psInBuf); 125 psInBuf = NULL; 126 free(psOutBuf); 127 psOutBuf = NULL; 128 129 return iOutLen; 130 131 } 132 133 int main() 134 { 135 char strUTF[]={ 136 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 137 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 138 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 139 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 140 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 141 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 142 0x00, 0x00, 0x00 143 }; 144 char chTmpStr[256]; 145 int len = 0; 146 147 memset(chTmpStr, 0x0, 256); 148 MAIN_UTF2Gb2312(strUTF, chTmpStr, 256); 149 printf("Main: change=%s\n", chTmpStr); 150 151 return 0; 152 }