导航

Code UTF-8 Console GB2312 Linux 中文乱码

Posted on 2021-06-04 17:36  DotNet1010  阅读(117)  评论(0编辑  收藏  举报

Linux 系统方法:  LD_LIBRARY_PATH=. ./userdemo | iconv -f GB2312 -t utf8   

Shell 方法  Shell 编码 改成GB2312

 

// 编码转换
//https://www.cnblogs.com/Ivanhan2019/p/11633247.html
int code_convert(const  char *from_charset,const  char *to_charsetchar *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
iconv_t cd;
//int rc;
char **pin = &inbuf;
char **pout = &outbuf;

cd = iconv_open(to_charset,from_charset);
if (cd==0return -1;
if (iconv(cd,pin,&inlen,pout,&outlen)==(size_t)-1return -1
iconv_close(cd);
return 0;
}

// utf8码转为GB2312码
int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
{
return code_convert("utf-8",  "gb2312",inbuf,inlen,outbuf,outlen);
}

// GB2312码转为utf8
int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
{
   return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
}

// utf8转为gbk
string u2a(const char *inbuf)
{
int inlen=strlen(inbuf);
string strRet;
strRet.resize(inlen*2+2);
if(code_convert("utf-8","gbk",const_cast<char *>(inbuf),inlen,&strRet[0],strRet.size()))
return inbuf;
return strRet;
}

// gbk转为utf8
string a2u(const char *inbuf)
{
int inlen=strlen(inbuf);
string strRet;
strRet.resize(inlen*2+2);
if(code_convert("gbk","utf-8",const_cast<char *>(inbuf),inlen,&strRet[0],strRet.size()))
return inbuf;
return strRet;
}
 
 
  int char_length=255;   char out1[char_length];
      if(g2u(pRspInfo->ErrorMsg,strlen(pRspInfo->ErrorMsg),out1,char_length)!=-1)
      {
             strcpy(pRspInfo->ErrorMsg,out1);
             //{OnRspOrderAction} Fail ErrorID:1172, ErrorMsg:该报单不存在 nRequestID:1 Chain:1
             PRINT_RSP_ERR(pRspInfo);
      }