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. CCF真题之节日

    201503-3 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ y ...

  2. kafka 0.8.x producer Example(scala)

    Producer 最简配置 metadata.broker.list参数指定broker地址,这里不需要填上所有的broker地址,但是如果只写一个,这个broker挂掉后就无法往topic中写入信息 ...

  3. windows 下双网卡在不同网络切换设置

           首先你的机器需要有两块网卡,分别接到两台交换机上, ine rnet地址:192.168.1.8,子网掩码:255.255.255.0,网关:192.168.1.1 内部网地址:172. ...

  4. struct2 学习总结

    花了近半个月学习了struct2.现大致总结下学习点: 1. struct2 入门以及基本配置(未继承ActionSupport,配置struts.xml文件,execute方法直接返回SUCESS) ...

  5. Spring+SpringMVC+MyBatis)

    用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的 ...

  6. 视频处理控件TVideoGrabber如何重新编码视频/音频(2)

    在前面的文中<视频处理控件TVideoGrabber如何重新编码视频>已经讲解了部分TVideoGrabber重新编码音频.视频剪辑的内容,下面将继续说明. 重新编码进程 重新编码开始时, ...

  7. java总结第二次//数组及面向对象

    三.java数组 主要内容:数组概述.一维数组声明.数组元素的引用.数组元素的默认初始化.创建数组.数组初始化.多维数组.多维数组初始化.数组排序 1.数组概述 数组是多个相同类型数据的组合,实现对这 ...

  8. oracle SGA详解

    SGA(System Global Area)系统全局区.这是一个非常庞大的内存区间,也是为什么开启oracle之后占用了很大内存的原因. SGA分为不同的池,我们可以通过视图v$sgastat查看, ...

  9. mybatis n+1问题

    mybatis的一对多或者多对多的时候,2中方式解决,一种是嵌套select,但是会有n+1问题,不推荐:另外一种是使用一条sql,在该sql里面使用子查询的方式来完成.比如 select * fro ...

  10. 内存泄露:*.hprof

    使用Memory Analyzer tool(MAT)分析内存泄漏 转账地址:http://www.blogjava.net/rosen/archive/2010/06/13/323522.html ...