Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1770206
  • 博文数量: 198
  • 博客积分: 4088
  • 博客等级: 上校
  • 技术积分: 2391
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-15 16:29
个人简介

游戏开发,系统架构; 博客迁移到:http://www.jianshu.com/u/3ac0504b3b8c

文章分类

全部博文(198)

文章存档

2017年(1)

2016年(12)

2015年(1)

2014年(3)

2013年(13)

2012年(18)

2011年(150)

分类: C/C++

2012-03-06 10:07:24

  1. int chr2wch(const char* buffer, wchar_t* &wBuf)
  2. {
  3.       size_t len = strlen(buffer);

  4.       size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), NULL, 0);

  5.       wBuf = new wchar_t[wlen + 1];

  6.       MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), wBuf, int(wlen));
  7.      wBuf[wlen] = 0;

  8.      return (int)wlen;
  9. }

  10. int chr2utf(const char* buf, char* &mb_buf)
  11. {
  12.     WCHAR* wbuf;
  13.     int wlen = chr2wch(buf, wbuf);
  14.     int mb_buf_size = ::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, NULL, 0, NULL, NULL);
  15.     mb_buf = new char[mb_buf_size + 1];
  16.     int mb_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, mb_buf, mb_buf_size, NULL, NULL);        
  17.     mb_buf[mb_len] = 0;
  18.     delete[] wbuf;
  19.     return mb_len;
  20. }


  21. int utf2wch(const char* buffer, wchar_t* &wBuf)
  22. {
  23.     size_t len = strlen(buffer);

  24.     size_t wlen = MultiByteToWideChar(CP_UTF8, 0, (const char*)buffer, int(len), NULL, 0);

  25.     wBuf = new wchar_t[wlen + 1];

  26.     MultiByteToWideChar(CP_UTF8, 0, (const char*)buffer, int(len), wBuf, int(wlen));
  27.     wBuf[wlen] = 0;

  28.     return (int)wlen;
  29. }

  30. int utf2chr(const char* buf, char* &mb_buf)
  31. {
  32.     WCHAR* wbuf;
  33.     int wlen = utf2wch(buf, wbuf);
  34.     int mb_buf_size = ::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen, NULL, 0, NULL, NULL);
  35.     mb_buf = new char[mb_buf_size + 1];
  36.     int mb_len = ::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen, mb_buf, mb_buf_size, NULL, NULL);        
  37.     mb_buf[mb_len] = 0;
  38.     delete[] wbuf;
  39.     return mb_len;
  40. }

由于保存新的字符串的内存是堆上创建的,所以用完之后要 delete []out_msg;

阅读(1186) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~