设为首页 加入收藏

TOP

C++中各种string的相互转化(一)
2013-10-17 09:03:49 来源: 作者: 【 】 浏览:594
Tags:各种 string 相互 转化

  一 C++ 中 string与wstring互转

  方法一:

  string WideToMutilByte(const wstring& _src)

  {

  int nBufSize = WideCharToMultiByte(GetACP(), 0, _src.c_str(),-1, NULL, 0, 0, FALSE);

  char *szBuf = new char[nBufSize];

  WideCharToMultiByte(GetACP(), 0, _src.c_str(),-1, szBuf, nBufSize, 0, FALSE);

  string strRet(szBuf);

  delete []szBuf;

  szBuf = NULL;

  return strRet;

  }

  wstring MutilByteToWide(const string& _src)

  {

  //计算字符串 string 转成 wchar_t 之后占用的内存字节数

  int nBufSize = MultiByteToWideChar(GetACP(),0,_src.c_str(),-1,NULL,0);

  //为 wsbuf 分配内存 BufSize 个字节

  wchar_t *wsBuf = new wchar_t[nBufSize];

  //转化为 unicode 的 WideString

  MultiByteToWideChar(GetACP(),0,_src.c_str(),-1,wsBuf,nBufSize);

  wstring wstrRet(wsBuf);

  delete []wsBuf;

  wsBuf = NULL;

  return wstrRet;

  }

  转载:csdn

  这篇文章里,我将给出几种C++ std::string和std::wstring相互转换的转换方法。

  第一种方法:调用WideCharToMultiByte()和MultiByteToWideChar(),代码如下(关于详细的解释,可以参考《windows核心编程》):

  #include <string>

  #include <windows.h>

  using namespace std;

  //Converting a WChar string to a Ansi string

  std::string WChar2Ansi(LPCWSTR pwszSrc)

  {

  int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);

  if (nLen<= 0) return std::string("");

  char* pszDst = new char[nLen];

  if (NULL == pszDst) return std::string("");

  WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);

  pszDst[nLen -1] = 0;

  std::string strTemp(pszDst);

  delete [] pszDst;

  return strTemp;

  }

  string ws2s(wstring& inputws)

  {

  return WChar2Ansi(inputws.c_str());

  }

  //Converting a Ansi string to WChar string

  std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)

  {

  int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);

  if(nSize <= 0) return NULL;

  WCHAR *pwszDst = new WCHAR[nSize+1];

  if( NULL == pwszDst) return NULL;

  MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);

  pwszDst[nSize] = 0;

  if( pwszDst[0] == 0xFEFF)                    // skip Oxfeff

  for(int i = 0; i < nSize; i ++)

  pwszDst[i] = pwszDst[i+1];

  wstring wcharString(pwszDst);

  delete pwszDst;

  return wcharString;

  }

  std::wstring s2ws(const string& s)

  {

  return Ansi2WChar(s.c_str(),s.size());

  }

  第二种方法:采用ATL封装_bstr_t的过渡:(注,_bstr_是Microsoft Specific的,所以下面代码可以在VS2005通过,无移植性);

  #include <string>

  #include <comutil.h>

  using namespace std;

  #pragma comment(lib, "comsuppw.lib")

  string ws2s(const wstring& ws);

  wstring s2ws(const string& s);

  string ws2s(const wstring& ws)

  {

  _bstr_t t = ws.c_str();

  char* pchar = (char*)t;

  string result = pchar;

  return result;

  }

  wstring s2ws(const string& s)

  {

  _bstr_t t = s.c_str();

  wchar_t* pwchar = (wchar_t*)t;

  wstring result = pwchar;

  return result;

  }

  第三种方法:使用CRT库的mbstowcs()函数和wcstombs()函数,平台无关,需设定locale.

  #include <string>

  #include <locale.h>

  using namespace std;

  string ws2s(const wstring& ws)

  {

  string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";

  setlocale(LC_ALL, "chs");

  const wchar_t* _Source = ws.c_str();

  size_t _Dsize = 2 * ws.size() + 1;

  char *_Dest = new char[_Dsize];

  memset(_Dest,0,_Dsize);

  wcstombs(_Dest,_Source,_Dsize);

  string result = _Dest;

  delete []_Dest;

  setlocale(LC_ALL, curLocale.c_str());

  return result;

  }

  wstring s2ws(const string& s)

  {

  setlocale(LC_ALL, "chs");

  const char* _Source = s.c_str();

  size_t _Dsize = s.size() + 1;

  wchar_t *_Dest = new wchar_t[_Dsize];

  wmemset(_Dest, 0, _Dsize);

  mbstowcs(_Dest,_Source,_Dsize);

  wstring result = _Dest;

  delete []_Dest;

  setlocale(LC_ALL, "C");

  return result;

  }

  二 utf8.utf16.utf32的相互转化

  可以参考Unicode.org 上有ConvertUTF.c和ConvertUTF.h (下载地址:http://www.unicode.org/Public/PROGRAMS/CVTUTF/

  实现文件ConvertUTF.c:(。h省)

  /*

  * Copyright 2001-2004 Unicode, Inc.

  *

  * Disclaimer

  *

  * This source code is provided as is by Unicode, Inc. No claims are

  * made as to fitness for any particular purpose. No warranties of any

  * kind are expressed or implied. The recipient agrees to determine

  * applicability of information provided. If this file has been

  * purchased on magnetic or optical media from Unicode, Inc., the

  * sole remedy for any claim will be exchange of defective media

  * within 90 days of receipt.

  *

  * Limitations on Rights to Redistribute This Code

  *

  * Unicode, Inc. hereby grants the right to freely use the information

  * supplied in this file in the creation of products supporting the

  * Unicode Standard, and to make copies of this file in any form

  * for internal or external distribution as long as this notice

  * remains attached.

  */

  /* ---------------------------------------------------------------------

  Conversions between UTF32, UTF-16, and UTF-8. Source code file.

  Author: Mark E. Davis, 1994.

  Rev History: Rick McGowan, fixes & updates May 2001.

  Sept 2001: fixed const & error conditions per

  mods suggested by S. Parent & A. Lillich.

  June 2002: Tim Dodd added detection and handling of incomplete

  source sequences, enhanced error detection, added casts

  to eliminate compiler warnings.

  July 2003: slight mods to back out aggressive FFFE detection.

  Jan 2004: updated switches in from-UTF8 conversions.

  Oct 2004: updated to use UNI_MAX_LEGAL_UTF32 in UTF-32 conversions.

  See the header file "ConvertUTF.h" for complete documentation.

  ------------------------------------------------------------------------ */

  #include "ConvertUTF.h"

  #ifdef CVTUTF_DEBUG

  #include <stdio.h>

  #endif

  static const int halfShift  = 10; /* used for shifting by 10 bits */

  static const UTF32 halfBase = 0x0010000UL;

  static const UTF32 halfMask = 0x3FFUL;

  #define UNI_SUR_HIGH_START  (UTF32)0xD800

  #define UNI_SUR_HIGH_END    (UTF32)0xDBFF

  #define UNI_SUR_LOW_START   (UTF32)0xDC00

  #define UNI_SUR_LOW_END     (UTF32)0xDFFF

  #define false       0

  #define true        1

  /* --------------------------------------------------------------------- */

  ConversionResult ConvertUTF32toUTF16 (

  const UTF32** sourceStart, const UTF32* sourceEnd,

  UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags) {

  ConversionResult result = conversionOK;

  const UTF32* source = *sourceStart;

  UTF16* target = *targetStart;

  while (source < sourceEnd) {

  UTF32 ch;

  if (target >= targetEnd) {

  result = targetExhausted; break;

  }

  ch = *source++;

  if (ch <= UNI_MAX_BMP) { /* Target is a character <= 0xFFFF */

  /* UTF-16 surrogate values are illegal in UTF-32; 0xffff or 0xfffe are both reserved values */

  if (ch >= UNI_SUR_HIGH_START && ch <= UNI_SUR_LOW_END) {

  if (flags == strictConversion) {

  --source; /* return to the illegal value itself */

  result = sourceIllegal;

  break;

  } else {

  *target++ = UNI_REPLACEMENT_CHAR;

  }

  } else {

  *target++ = (UTF16)ch; /* normal case */

  }

  } else if (ch > UNI_MAX_LEGAL_UTF32) {

  if (flags == strictConversion) {

  result = sourceIllegal;

  } else {

  *target++ = UNI_REPLACEMENT_CHAR;

  }

  } else {

  /* target is a character in range 0xFFFF - 0x10FFFF. */

  if (target + 1 >= targetEnd) {

  --source; /* Back up source pointer! */

  result = targetExhausted; break;

  }

       

首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++接口与实现分离的2种方法 下一篇转盘旋转算法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: