Windows客户端 Linux服务器通讯 字符编码问题
2024-09-03 22:14:02
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("转码失败,转码缓冲不够\n系统错误信息:%m\n");
return false;
}
case EILSEQ:
{
printf("转码失败(有可能同码相转)\n系统错误信息:%m\n");
return false;
}
default:
{
printf("转码失败\n系统错误信息:%m\n");
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);
}
最新文章
- Kubernets搭建Kubernetes-dashboard
- 总结Oracle删除表空间遇到的问题
- WORDPRESS点击标题或图片无法链接到文章页面
- .net之工作流工程展示及代码分享(一)工作流表单
- PHP实现发红包程序
- C++析构函数调用异常问题研究
- asp.net中导出Excel的方法
- socket本地模拟TCP 服务器+客户端(二)
- 行为驱动开发(BDD)
- xamarin android checkbox自定义样式
- Python 中的继承、多态和封装
- 索引法则--IS NULL, IS NOT NULL 也无法使用索引
- 并发库应用之十二 &; 常用集合问题汇总
- Python合并多个Excel数据
- 工作中bug笔记
- 2.5 Apache Axis2 快速学习手册之JiBx 构建Web Service
- 使用Python + Selenium破解滑块验证码
- python中函数嵌套、函数作为变量以及闭包的原理
- IOS 7层协议
- CGLIB代理基础
热门文章
- 执行dlsym()函数出现: undefined symbol
- IDA Pro - 使用IDA Pro逆向C++程序
- java 学习笔记(四) java连接ZooKeeper
- linux ngxtop安装安装及使用
- Gym - 102082G What Goes Up Must Come Down (树状数组+贪心)
- uniapp开发微信小程序跳转出现navigateTo:fail page ";pages/user/pages/user/address/address"; is not found
- 【leetcode】Department Top Three Salaries
- Python基础-day05
- HTML的状态码
- TTTTTTTTTTTTT CF Good Bye 2015 C- New Year and Domino(CF611C) 二维前缀