Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1778892
  • 博文数量: 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)

分类: Windows平台

2013-04-24 13:43:06


  1. #include <Windows.h>
  2. #include "utility.h"
  3. // wchar_t* 转 char*
  4. int wch2chr(const wchar_t *wBuf, char* &mb_buf)
  5. {
  6.     int nlength= wcslen(wBuf);

  7.     int nbytes = WideCharToMultiByte( 0, 0, wBuf, nlength, NULL, 0, NULL, NULL );

  8.     if( nbytes > 2 * nlength ) nbytes = 2 * nlength;

  9.     mb_buf = new char[nbytes + 1];

  10.     WideCharToMultiByte( 0, 0 , wBuf, nlength, mb_buf, nbytes, NULL, NULL );

  11.     mb_buf[nbytes] = 0;

  12.     return nbytes;
  13. }
  14. // wchar_t* 转 char*
  15. void wch2chr_s(const wchar_t *wBuf, std::string& cstr)
  16. {
  17.     char* mb_buf = NULL;
  18.     int len = wch2chr(wBuf, mb_buf);

  19.     cstr = std::string(mb_buf);
  20.     delete[] mb_buf;
  21. }
  22. // char* 转 wchar_t*
  23. int chr2wch(const char* buffer, wchar_t* &wBuf)
  24. {
  25.     size_t len = strlen(buffer);

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

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

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

  30.     return (int)wlen;
  31. }
  32. // wchar_t* 转char*
  33. void chr2wch_s(const char* buffer, std::wstring& wcstr)
  34. {
  35.     wchar_t *wbuf = NULL;
  36.     int len = chr2wch(buffer, wbuf);

  37.     wcstr = std::wstring(wbuf);
  38.     delete[] wbuf;
  39. }
  40. //* gbk 转 utf-8
  41. int chr2utf(const char* buf, char* &mb_buf)
  42. {
  43.     WCHAR* wbuf;
  44.     int wlen = chr2wch(buf, wbuf);
  45.     int mb_buf_size = ::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, NULL, 0, NULL, NULL);
  46.     mb_buf = new char[mb_buf_size + 1];
  47.     int mb_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf, wlen, mb_buf, mb_buf_size, NULL, NULL);        
  48.     mb_buf[mb_len] = 0;
  49.     delete[] wbuf;
  50.     return mb_len;
  51. }
  52. // gbk 转 utf-8
  53. void chr2utf_s(const char* buf, std::string& cstr)
  54. {
  55.     char* mb_buf = NULL;
  56.     int len = chr2utf(buf, mb_buf);

  57.     cstr = std::string(mb_buf);
  58.     delete[] mb_buf;
  59. }
  60. // utf-8 转 wchar_t*
  61. int utf2wch(const char* buffer, wchar_t* &wBuf)
  62. {
  63.     size_t len = strlen(buffer);

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

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

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

  68.     return (int)wlen;
  69. }
  70. // utf-8 转  wchar_t*
  71. void utf2wch_s(const char* buffer, std::wstring& wcstr)
  72. {
  73.     wchar_t* wbuf = NULL;
  74.     int len = utf2wch(buffer, wbuf);

  75.     wcstr = std::wstring(wbuf);
  76.     delete[] wbuf;
  77. }
  78. // utf-8 转 gbk
  79. int utf2chr(const char* buf, char* &mb_buf)
  80. {
  81.     WCHAR* wbuf;
  82.     int wlen = utf2wch(buf, wbuf);
  83.     int mb_buf_size = ::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen, NULL, 0, NULL, NULL);
  84.     mb_buf = new char[mb_buf_size + 1];
  85.     int mb_len = ::WideCharToMultiByte(CP_ACP, 0, wbuf, wlen, mb_buf, mb_buf_size, NULL, NULL);        
  86.     mb_buf[mb_len] = 0;
  87.     delete[] wbuf;
  88.     return mb_len;
  89. }
  90. // utf8 转 gbk
  91. void utf2chr_s(const char* buf, std::string& cstr)
  92. {
  93.     char* mb_buf = NULL;
  94.     int len = utf2chr(buf, mb_buf);

  95.     cstr = std::string(mb_buf);
  96.     delete[] mb_buf;
  97. }

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