Chinaunix首页 | 论坛 | 博客
  • 博客访问: 328323
  • 博文数量: 64
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 589
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-08 15:50
文章分类

全部博文(64)

文章存档

2015年(52)

2014年(3)

2013年(9)

我的朋友

分类: WINDOWS

2015-07-20 17:01:47

//在windows与linux系统通过socket的传输数据里,如果传输中文字符,因为windows和linux使用的编码不同,所以需要将windows下的中文编码转换为linux使用的utf8格式的,否则会出现乱码
//m_string是windows下的中文字符串
//utf8_string是返回转换为utf8编码的中文字符串
//slen是utf8_string字符数组的大小
#include
int multichar_2_utf8(const char *m_string,char *utf8_string,int slen)
{
int len=0;
wchar_t *w_string;
//char *utf8_string;
    //计算由ansi转换为unicode后,unicode编码的长度
len=MultiByteToWideChar(CP_ACP,0,(char *)m_string, -1, NULL,0);//cp_acp指示了转换为unicode编码的编码类型
w_string=(wchar_t *)malloc(2*len+2);
memset(w_string,0,2*len+2);
    //ansi到unicode转换
MultiByteToWideChar(CP_ACP, 0, (char *)m_string,-1,w_string, len);//cp_acp指示了转换为unicode编码的编码类型
    //计算unicode转换为utf8后,utf8编码的长度
    //len = WideCharToMultiByte(cp_utf8, 0, w_string, -1, NULL, 0, NULL, NULL);//cp_utf8指示了unicode转换为的类型
    //utf8_string=(char  *)malloc(len+1);
   //  memset(utf8_string, 0, len + 1);
    //unicode到utf8转换
    //WideCharToMultiByte(CP_UTF8, 0, w_string, -1, utf8_string, len, NULL,NULL);//cp_utf8指示了unicode转换为的类型
    WideCharToMultiByte(CP_UTF8, 0, w_string, -1, utf8_string, slen, NULL,NULL);//cp_utf8指示了unicode转换为的类型
free(w_string);
return 0;
}
阅读(3826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~