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. session 和 cookie 的区别与联系

    1.创建一个新的Cookie Cookie cookie = new Cookie("username",name); 2.设置cookie在客户端上存活多久 cookie.set ...

  2. C#写好的类库dll怎么在别人调用的时候也能看到注释?

    菜单 Project -> 'xxxx' Properties -> Build -> Output -> 勾上 XML Documentation file

  3. paper 54 :图像频率的理解

    我一直在思考一个问题,图像增强以后,哪些方面的特征最为显著,思来想去,无果而终!翻看了一篇知网的paper,基于保真度(VIF)的增强图像质量评价,文章中指出无参考质量评价,可以从三个方面考虑:平均梯 ...

  4. 夺命雷公狗—angularjs—3—表单验证的高级用法

    其实我们的angularjs都是是块状代码,其实是可以在实际开发中保存下来以后就可以达到重复利用的目的了.. 废话不多说,直接上代码: <!doctype html> <html l ...

  5. 夺命雷公狗ThinkPHP项目之----企业网站17之网站配置页的添加

    为了网站可以智能一点,所以我们开始来写一个网站配置的功能.. 所以我来写他的数据表: 先来完成他的添加功能,页面效果如下所示: lists.html代码如下所示: <!doctype html& ...

  6. sp_addlinkedserver 方法应用

    EXEC  sp_addlinkedserver      @server='DBVIP',--被访问的服务器别名       @srvproduct='',      @provider='SQLO ...

  7. ckeditor添加插入flv视频的插件

    首发:个人博客,更新&纠错&回复 昨天写在网页中播放flv的博文的时候,想在博文中插入视频,但是发现无法实现.因为用的编辑器是ckeditor,决定自己写个插件插入视频.官方的教程在这 ...

  8. WM_SIZE

    procedure WMSize (var Message: TWMSize); message WM_SIZE; 参数说明 wParam: Specifies the type of resizin ...

  9. 危险的 SQL

    看下这个 SQL , 有什么问题 ? <update id="update" parameterType="CreativeGroupDO"> up ...

  10. NIOS II开发备忘录

    大概有一年没做NIOS II的开发了,回想上一次做NIOS II还是去年做毕业设计的时候.那时候做的是基于SOPC的频率特性测试仪,我大约花了2个月的时间,从无到有的学习了NIOS II开发.学习过N ...