c++ 常用的一些字符转换

wstring string_to_wstring( string& s )
{
 if( s.empty() ) return _T(""); 
 int retval = ::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),0,0);
 wstring re;
 re.resize( retval );
 ::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),&re[0],re.size());
 return re;
}

/// 宽字符串 到 一般窄字符串
string wstring_to_string( wstring& ws )
{
   if( ws.empty() ) return "";
   int retval = ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),0,0,0,0);
   string re;
   re.resize( retval );   ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0);
   return re;
}

/// 宽字符串 到 一般窄字符串
string wstring_to_string( wchar_t* ws, size_t sz )
{
   if( ws == NULL || sz == 0 ) return "";
   int retval = ::WideCharToMultiByte(CP_ACP,0,ws,sz,0,0,0,0);
   string re;
   re.resize( retval );   ::WideCharToMultiByte(CP_ACP,0,ws,sz,&re[0],re.size(),0,0);
   return re;
}

/// 窄字符串 到 宽字符串
wstring utf8_to_wstring( string& s )
{
   if( s.empty() ) return _T(""); 
   int retval = ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),0,0);
   wstring re;
   re.resize( retval );   ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),&re[0],re.size());
   return re;
}

/// 宽字符串 到 UTF8
string wstring_to_utf8( wstring& ws )
{
   if( ws.empty() ) return ""; 
   int retval = ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),0,0,0,0);
   string re;
   re.resize( retval );   ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0);
   return re;
}

string wstring_to_utf8( const wchar_t* ws, size_t sz )
{
   if( ws == NULL || sz == 0 ) return "";

   int retval = ::WideCharToMultiByte(CP_UTF8,0,ws,sz,0,0,0,0);
   string re; 
   re.resize( retval );   ::WideCharToMultiByte(CP_UTF8,0,ws,sz,&re[0],re.size(),0,0);
   return re;
}

string from_base64(string &base64)
{
   int nSize = ATL::Base64DecodeGetRequiredLength(base64.size());
   byte*  pData = new byte[nSize+1];
   ATL::Base64Decode(base64.c_str(), base64.size(), pData, &nSize);
   pData[nSize] = 0;

   string str = (char *)pData;
   delete []pData;
   return wstring_to_string(utf8_to_wstring(str));
}

wstring utf8_to_unicode(const string& utf8string)
{
   int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
   if (widesize == ERROR_NO_UNICODE_TRANSLATION)
   {
      throw exception("Invalid UTF-8 sequence.");
   }
   if (widesize == 0)
   {
   throw exception("Error in conversion.");
   } 
   vector<wchar_t> resultstring(widesize); 
   int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); 
   if (convresult != widesize)
   {
      throw exception("La falla!");
   } 
   return wstring(&resultstring[0]);
}

wstring acsi_to_widebyte(string& strascii)
{
   int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
   if (widesize == ERROR_NO_UNICODE_TRANSLATION)
   {
      throw exception("Invalid UTF-8 sequence.");
   }
   if (widesize == 0)
   {
      throw exception("Error in conversion.");
   }
   vector<wchar_t> resultstring(widesize);
   int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);


   if (convresult != widesize)
   {
      throw exception("La falla!");
   }

   return wstring(&resultstring[0]);
   }

   string unicode_to_utf8(const wstring& widestring)
   {
   int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
   if (utf8size == 0)
   {
      throw exception("Error in conversion.");
   }

   vector<char> resultstring(utf8size);

   int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);

   if (convresult != utf8size)
   {
      throw exception("La falla!");
   } 

   return string(&resultstring[0]);
}

string ascii_to_utf8(string& strAsciiCode)
{
   string strRet("");
   //先把 ascii 转为 unicode
   wstring wstr = acsi_to_widebyte(strAsciiCode);
   //最后把 unicode 转为 utf8
   strRet = unicode_to_utf8(wstr);
   return strRet;
}

int split( const string& str,vector<string>& subs, const char& sep /* = */ )
{
   string temp = str;
   temp += sep;
   int count = 0;

   size_t pos = temp.find( sep );
   size_t from = 0;
   while ( pos != temp.npos )
   {
      string sub_str = temp.substr( from, pos - from );
      //if ( sub_str.length() > 0 )
     {
           ++ count;
           subs.push_back( sub_str );
     }
     from = pos + 1;
     pos = temp.find( sep, from );
   }

   return count;
}

int splitw( const wstring& str, vector<wstring>& subs, const wchar_t& sep /* = */ )
{
   wstring temp = str;
   temp += sep;
   int count = 0;
   size_t pos = temp.find( sep );
   size_t from = 0;
   while ( pos != temp.npos )
   {
       wstring sub_str = temp.substr( from, pos - from );
       //if ( sub_str.length() > 0 )
       {
           ++ count;
           subs.push_back( sub_str );
        }
        from = pos + 1;
        pos = temp.find( sep, from );
    }

     return count;
}

///< 字符串替换
string& replace_all_s(string& str, const string& old_value,const string& new_value)
{
   while(true)
  { 
      string::size_type pos(0);
      if( (pos = str.find( old_value ) )!=string::npos )
         str.replace(pos,old_value.length(),new_value);
      else
         break;
  }
  return str;
}

wstring& replace_all_ws(wstring& str, const wstring& old_value,const wstring& new_value)
{
   while(true)
   {
      wstring::size_type pos(0);
      if( (pos = str.find( old_value ) )!=wstring::npos )
         str.replace(pos,old_value.length(),new_value);
      else
         break;
   }
   return str;
}

string& replace_all_distinct_s(string& str,const string& old_value,const string& new_value)
{
   for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length())
   {
         if( (pos=str.find(old_value,pos))!=string::npos )
            str.replace(pos,old_value.length(),new_value);
         else break;
   }
   return str;
}

wstring& replace_all_distinct_ws(wstring& str,const wstring& old_value,const wstring& new_value)
{
   for(wstring::size_type pos(0); pos!=wstring::npos; pos+=new_value.length() )
   {
      if( (pos=str.find(old_value,pos))!=wstring::npos )
         str.replace(pos,old_value.length(),new_value);
      else
         break;
   }
      return str;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值