string U2A(const wstring& str)//Unicode字符转Ascii字符
{
string strDes;
if ( str.empty() )
goto __end;
int nLen=::WideCharToMultiByte(CP_ACP, , str.c_str(), str.size(), NULL, , NULL, NULL);
if ( ==nLen )
goto __end;
char* pBuffer=new char[nLen+];
memset(pBuffer, , nLen+);
::WideCharToMultiByte(CP_ACP, , str.c_str(), str.size(), pBuffer, nLen, NULL, NULL);
pBuffer[nLen]='\0';
strDes.append(pBuffer);
delete[] pBuffer;
__end:
return strDes;
} wstring A2U(const string& str)//Ascii字符转
{
wstring strDes;
if ( str.empty() )
goto __end;
int nLen=::MultiByteToWideChar(CP_ACP, , str.c_str(), str.size(), NULL, );
if ( ==nLen )
goto __end;
wchar_t* pBuffer=new wchar_t[nLen+];
memset(pBuffer, , nLen+);
::MultiByteToWideChar(CP_ACP, , str.c_str(), str.size(), pBuffer, nLen);
pBuffer[nLen]='\0';
strDes.append(pBuffer);
delete[] pBuffer;
__end:
return strDes;
} string U2Utf(const wstring& wstrUnicode)//Unicode转utf8
{
string strRet;
if( wstrUnicode.empty() )
return strRet;
int nLen = WideCharToMultiByte(CP_UTF8, , wstrUnicode.c_str(), -, NULL, , NULL, NULL);
char* pBuffer=new char[nLen+];
pBuffer[nLen] = '\0';
nLen = WideCharToMultiByte(CP_UTF8, , wstrUnicode.c_str(), -, pBuffer, nLen, NULL, NULL);
strRet.append(pBuffer);
delete[] pBuffer;
return strRet;
} wstring Utf2U(const string &str)//utf8转Unicode
{
int u16Len = ::MultiByteToWideChar(CP_UTF8, NULL,str.c_str(),(int)str.size(), NULL, );
wchar_t* wstrBuf = new wchar_t[u16Len + ];
::MultiByteToWideChar(CP_UTF8, NULL, str.c_str(),(int)str.size(), wstrBuf, u16Len);
wstrBuf[u16Len] = L'\0';
wstring wStr;
wStr.assign(wstrBuf, u16Len);
delete [] wstrBuf;
return wStr;
}
//分割字符串
bool SplitString(const wstring& strSource,const wstring& strFlag, vector<wstring>& paramList)
{
if ( strSource.empty() || strFlag.empty() )
return false;
paramList.clear();
size_t nBeg = ;
size_t nFind = strSource.find(strFlag, nBeg);
if ( nFind == std::wstring::npos )
paramList.push_back(strSource);
else
{
while ( true )
{
if ( nFind != nBeg )
paramList.push_back(strSource.substr(nBeg, nFind-nBeg));
nBeg = nFind + strFlag.size();
if ( nBeg == strSource.size() )
break;
nFind = strSource.find(strFlag, nBeg);
if ( nFind == std::wstring::npos )
{
paramList.push_back(wstring(strSource.begin()+nBeg, strSource.end()));
break;
}
}
}
return true;
}
//URL编码
string UrlEncode(const string& strSrc)
{
string strDes;
for ( size_t i=; i<strSrc.size(); ++i )
{
BYTE ch=(BYTE)strSrc[i];
if ( isalnum(ch) || ch=='-' || ch=='_' || ch=='.' || ch=='~' )
strDes+=ch;
else if ( ch==' ' )
strDes+='+';
else
{
strDes+='%';
strDes+=ToHex( (ch>>) );
strDes+=ToHex( ch% );
}
}
return strDes;
}
//URL解码
string UrlDecode(const string& strSrc)
{
string strDes;
for ( size_t i = ; i < strSrc.size(); i++ )
{
BYTE ch=strSrc[i];
if (ch == '+')
strDes+=' ';
else if (ch == '%')
{
BYTE h = FromHex((unsigned char)strSrc[++i]);
BYTE l = FromHex((unsigned char)strSrc[++i]);
strDes += (h<<) + l;
}
else strDes += ch;
}
return strDes;
}
//替换字符串
wstring StrReplaceW(const wstring& strContent, const wstring& strTag, const wstring& strReplace)
{
size_t nBegin=, nFind=;
nFind = strContent.find(strTag, nBegin);
if ( nFind == wstring::npos )
return strContent;
size_t nTagLen = strTag.size();
wstring strRet;
while ( true )
{
strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
strRet.append(strReplace);
nBegin = nFind + nTagLen;
nFind = strContent.find(strTag, nBegin);
if ( nFind == wstring::npos )
{
strRet.append(strContent.begin()+nBegin, strContent.end());
break;
}
}
return strRet;
} string StrReplaceA( const string& strContent, const string& strTag, const string& strReplace )
{
size_t nBegin=, nFind=;
nFind = strContent.find(strTag, nBegin);
if ( nFind == string::npos )
return strContent;
size_t nTagLen = strTag.size();
string strRet;
while ( true )
{
strRet.append(strContent.begin()+nBegin, strContent.begin()+nFind);
strRet.append(strReplace);
nBegin = nFind + nTagLen;
nFind = strContent.find(strTag, nBegin);
if ( nFind == string::npos )
{
strRet.append(strContent.begin()+nBegin, strContent.end());
break;
}
}
return strRet;
}

转载:http://blog.csdn.net/mfcing/article/details/7529848

c++中char*\wchar_t*\string\wstring之间的相互转换的更多相关文章

  1. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...

  2. 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换

    本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下:    #ifndef USE_H_     ...

  3. wchar_t char string wstring 之间的转换

    wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...

  4. c++ 中 char 与 string 之间的相互转换问题

    第一部分: 将  char *    或者    char []   转换为  string 可以直接赋值,转换. 第二部分: 将   string   转换为 char *    或者    cha ...

  5. Java中char和String的相互转换

    转自:http://blog.csdn.net/yaokai_assultmaster/article/details/52082763 Java中char是一个基本类型,而String是一个引用类型 ...

  6. C#中char[]与string之间的转换;byte[]与string之间的转化

    目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...

  7. C# Enum Name String Description之间的相互转换

    最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...

  8. JAVA中char和String/值类型和引用类型的区别

    import java.util.*; class test { public static void main(String[] args) { char a[] = {'b', 'a', 'c'} ...

  9. 包装类、基本数据类型及String类之间的相互转换

    包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器      int i=10;     Inte ...

随机推荐

  1. c++中的传参问题

    从概念上讲.指针从本质上讲就是存放变量地址的一个变量,在逻辑上是独立的,它可以被改变,包括其所指向的地址的改变和其指向的地址中所存放的数据的改变. 而引用是一个别名,它在逻辑上不是独立的,它的存在具有 ...

  2. apache高负载性能调优

    先阅读apache配置优化建议如下,再对相关参数进行调整,观察服务器状况.Apache配置优化建议:进入/usr/local/apache2/conf/extra 目录下Apache优化,经过上述操作 ...

  3. 夺命雷公狗jquery---6属性选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. [php] 判断当前运行模式

    //判断是否cgi模式 define('IS_CGI',substr(PHP_SAPI, 0,3)=='cgi' ? 1 : 0 ); //判断操作系统是否为windows define('IS_WI ...

  5. html5,表格与框架综合布局

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. js获取单选框radio的值

    遇到一个js获取radio值的问题,原来根据frm.type.value取到的值在ie下是空值 解决办法:type为每个radio的值 var chkObjs=document.getElements ...

  7. Ceph的集群全部换IP

    由于要对物理机器要做IP规划,所有物理机统一做到35网段,对于ceph集群来说,是有一定工作量的. 前提条件,ceph集群正常.原来的所有集群在44网段.mon地址是172.17.44.22 在44网 ...

  8. UITableView(转)

    一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示:          其中左边的是Plain风格的,右 ...

  9. android 项目学习随笔一(闪屏 )

    1.取标题栏且全屏 <activity android:name="com.ecollab.zhsh66.SplashActivity" android:label=&quo ...

  10. linux 集群配置ssh无密码访问

    一.修改host文件 1) 用客户端工具(ssh client或者putty)连接到linux服务器.在root用户下输入命令 vi /etc/hosts,用vi编辑hosts文件,如下: #127. ...