C语言-字符编码转换:UTF与GB2312

时间:2025-01-23 16:04:26

依赖库libiconv,libiconv库的交叉编译不做描述,网上很多

 #include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "iconv.h"
#include "eventlist.h" static int ChangeCode( const char* pFromCode, const char* pToCode, const char* pInBuf,
size_t* piInLen, char* pOutBuf, size_t* piOutLen )
{
int iRet;
char **pin = &pInBuf;
char **pout = &pOutBuf;
iconv_t hIconv; //printf("%s: outlen=%d, inlen=%d\n", __FUNCTION__, *piOutLen, *piInLen); //打开字符集转换
hIconv = iconv_open( pToCode, pFromCode );
if ( - == (int)hIconv )
{
perror("iconv_open");
return -;
}
//开始转换
printf("%s: 1 outlen=%d\n", __FUNCTION__, *piOutLen);
iRet = iconv( hIconv, pin, piInLen, pout, piOutLen );
if ( - == iRet )
{
perror("iconv");
return -;
}
printf("%s: 2 outlen=%d\n", __FUNCTION__, *piOutLen); //关闭字符集转换
iconv_close( hIconv ); **pout = '\0';
return iRet;
} static int MAIN_UTFToGb2312(char * pcFrom, char * pcTo, int iMaxToLen)
{
char * psInBuf = NULL;
char * psOutBuf = NULL;
unsigned int iInLen = ;
unsigned int iOutLen = ;
int iRet; iInLen = strlen(pcFrom)+;
psInBuf = (char *)malloc(iInLen);
if ( NULL == psInBuf )
{
return ;
}
memset(psInBuf, 0x0, iInLen);
memcpy(psInBuf, pcFrom, iInLen); iOutLen = iMaxToLen;
psOutBuf = (char *)malloc(iOutLen);
if ( NULL == psOutBuf )
{
free(psInBuf);
psInBuf = NULL;
return ;
}
memset(psOutBuf, 0x0, iOutLen); iRet = ChangeCode( "utf-8", "gb2312", psInBuf, &iInLen, psOutBuf, &iOutLen );
//iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen);
if ( != iRet )
{
printf("ChangeCode: Error\n");
//return 0;
}
memcpy(pcTo, psOutBuf, iOutLen);
printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); free(psInBuf);
psInBuf = NULL;
free(psOutBuf);
psOutBuf = NULL; return iOutLen; } static int MAIN_GB2312ToUTF(char * pcFrom, char * pcTo, int iMaxToLen)
{
char * psInBuf = NULL;
char * psOutBuf = NULL;
unsigned int iInLen = ;
unsigned int iOutLen = ;
int iRet; iInLen = strlen(pcFrom)+;
psInBuf = (char *)malloc(iInLen);
if ( NULL == psInBuf )
{
return ;
}
memset(psInBuf, 0x0, iInLen);
memcpy(psInBuf, pcFrom, iInLen); iOutLen = iMaxToLen;
psOutBuf = (char *)malloc(iOutLen);
if ( NULL == psOutBuf )
{
free(psInBuf);
psInBuf = NULL;
return ;
}
memset(psOutBuf, 0x0, iOutLen); iRet = ChangeCode( "gb2312", "utf-8", psInBuf, &iInLen, psOutBuf, &iOutLen );
//iRet = u2g(psInBuf, iInLen, psOutBuf, iOutLen);
if ( != iRet )
{
printf("ChangeCode: Error\n");
//return 0;
}
memcpy(pcTo, psOutBuf, iOutLen);
printf("%s: iOutLen = %d\n", __FUNCTION__, iOutLen); free(psInBuf);
psInBuf = NULL;
free(psOutBuf);
psOutBuf = NULL; return iOutLen; } int main()
{
char strUTF[]={
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6, 0xE5, 0xBC, 0x80, 0xE8,0xBD, 0xA6, 0xE4, 0xBA, 0x8B, 0xE4, 0xBB, 0xB6,
0x00, 0x00, 0x00
};
char chTmpStr[];
int len = ; memset(chTmpStr, 0x0, );
MAIN_UTF2Gb2312(strUTF, chTmpStr, );
printf("Main: change=%s\n", chTmpStr); return ;
}