WideCharToMultiByte和MultiByteToWideChar函数的使用方法

WideCharToMultiByteMultiByteToWideChar函数的使用方法。

多字节向宽字节转换:

 

 

  1. wstring converToWideChar( const string& str )
  2. {
  3.  int  len = 0;
  4.  len = str.length();
  5.  int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);  
  6.  wchar_t *  pUnicode;  
  7.  pUnicode = new  wchar_t[unicodeLen+1];  
  8.  memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
  9.  ::MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen);  
  10.  wstring  rt;  
  11.  rt = ( wchar_t* )pUnicode;
  12.  delete  pUnicode; 
  13.  return  rt;  
  14. }

宽字节向多字节转换:

  1. string converToMultiChar( const wstring& str )
  2. {
  3.  char*     pElementText;
  4.  int    iTextLen;
  5.  // wide char to multi char
  6.  iTextLen = WideCharToMultiByte( CP_ACP,
  7.          0,
  8.          str.c_str(),
  9.          -1,
  10.          NULL,
  11.          0,
  12.          NULL,
  13.          NULL );
  14.  pElementText = new char[iTextLen + 1];
  15.  memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
  16.  ::WideCharToMultiByte( CP_ACP,
  17.          0,
  18.          str.c_str(),
  19.          -1,
  20.          pElementText,
  21.          iTextLen,
  22.          NULL,
  23.          NULL );
  24.  string strText;
  25.  strText = pElementText;
  26.  delete[] pElementText;
  27.  return strText;
  28. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值