Windows客户端 Linux服务器通讯 字符编码问题

Windows下的字符编码默认是gb2312 在Linux下需要转成utf8 才能正确的看到对应的中文编码 提供转换函数

/*---------------------------------------------------
Date: 					2014-12-15
Author: 				fangjunmin
Modify:				
Description:			编码转换模块
 ----------------------------------------------------*/
#include "encode.h"
#include "stdio.h"
#include <errno.h>
#include <iconv.h>


bool ConvertEncode( const char *from_charset, const char *to_charset, char *inbuf , size_t inlen,char *outbuf, size_t& outlen )
{
    char *pin = inbuf;
    char *pout = outbuf;
    size_t ulInLen = inlen;
    size_t ulOutLen = outlen;

    iconv_t cd = iconv_open(to_charset, from_charset);
    if( cd == (iconv_t)(-1) )
    {
        return false;
    }

    memset(outbuf, 0, outlen);
    int nRet = iconv(cd, &pin, (size_t*)&ulInLen, &pout, (size_t*)&ulOutLen);
   	iconv_close(cd);	//切记

   	outlen -= ulOutLen;

   	if( nRet == -1 )
   	{
   		int nError = errno;
   		switch( nError)
   		{
   		case EINVAL:
   			{
   				return true;
   			}
   		case E2BIG:
			{
				printf("转码失败,转码缓冲不够
系统错误信息:%m
");
				return false;
			}
   		case EILSEQ:
			{
				printf("转码失败(有可能同码相转)
系统错误信息:%m
");
				return false;
			}
   		default:
			{
				printf("转码失败
系统错误信息:%m
");
				return false;
			}
   		}
   	}

   	return true;
}

bool GB2312ToUtf8(const char* szSrc, int iSrcLen, char* szDst, size_t& iDstLen)
 {
	return ConvertEncode("GB18030", "utf-8", (char*)szSrc, iSrcLen, szDst, iDstLen);
 }

  

原文地址:https://www.cnblogs.com/Jimmy104/p/5192687.html