c++中char*\wchar_t*\string\wstring之间的相互转换
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之间的相互转换的更多相关文章
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换 [转]
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下. #ifndef USE_H_ #define USE_H_ # ...
- 深入理解c++中char*与wchar_t*与string以及wstring之间的相互转换
本篇文章是对c++中的char*与wchar_t*与string以及wstring之间的相互转换进行了详细的分析介绍,需要的朋友参考下-复制代码 代码如下: #ifndef USE_H_ ...
- wchar_t char string wstring 之间的转换
wchar_t char string wstring 之间的转换 转:http://blog.csdn.net/lbd2008/article/details/8333583 在处理中文时有时需要进 ...
- c++ 中 char 与 string 之间的相互转换问题
第一部分: 将 char * 或者 char [] 转换为 string 可以直接赋值,转换. 第二部分: 将 string 转换为 char * 或者 cha ...
- Java中char和String的相互转换
转自:http://blog.csdn.net/yaokai_assultmaster/article/details/52082763 Java中char是一个基本类型,而String是一个引用类型 ...
- C#中char[]与string之间的转换;byte[]与string之间的转化
目录 1.char[]与string之间的转换 2.byte[]与string之间的转化 1.char[]与string之间的转换 //string 转换成 Char[] string str=&qu ...
- C# Enum Name String Description之间的相互转换
最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...
- JAVA中char和String/值类型和引用类型的区别
import java.util.*; class test { public static void main(String[] args) { char a[] = {'b', 'a', 'c'} ...
- 包装类、基本数据类型及String类之间的相互转换
包装类:8种基本数据类型对应一个类,此类即为包装类 一.基本数据类型 包装类 及String之间的转换 1.基本数据类型转化为包装类:调用包装类的构造器 int i=10; Inte ...
随机推荐
- Oracle EBS环境下查找数据源(OAF篇)
在用户层设置预制文件:Personalize Self-Service Defn 的值为Yes 来启动个性化模式 参考:http://www.2cto.com/database/201109/1041 ...
- android实操--练习1
这两天有空,打算把一些文档整理一下,快要考试了,找一些简单的例子来做做,重温安卓的知识. 下面是第一个练习: 实现很简单,下面我们来看看: 首先新建一个安卓项目Demo1 接着是界面的布局(包括act ...
- Intent跳转传list集合
先把List<>改为ArrayList<> ArrayList<Good> list=new ArrayList<Good>(); Intent int ...
- Mongodb 笔记07 分片、配置分片、选择片键、分片管理
分片 1. 分片(sharding)是指将数据拆分,将其分散存放在不同的机器上的过程.有时也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机就可以 ...
- 【python cookbook】【字符串与文本】4.文本模式的匹配和查找
问题:按照特定的文本模式进行匹配或查找 解决方法: 1.简单的文字匹配,只需使用str.find().str.startswith().str.endswith()或类似的函数即可: 2.复杂的匹配, ...
- grd文件结构
- centos7重启rsyslog服务|centos7重启syslog服务
centos7重启rsyslog服务: systemctl restart rsyslog 使用:(killall无效) killall -HUP rsyslog
- MySQL与SQL比较有那些区别呢
MySQL是一个逐渐完善的过程,使用前期版本时会遇到一些问题,通常搞得莫名其妙,在版本选择上尽量选择最新的. 1.在5.03以前版本中,存储varchar型数据时,后面的空格会被忽视掉,前面的空格会保 ...
- windows7配置Nginx+php+mysql的详细操作是怎样?
1.准备安装包等 (1)nginx-1.10.1.zip,下载链接为http://nginx.org/en/download.html .我个人选择了稳定版. (2)php-5.6.25-nts-Wi ...
- 【PHP设计模式 06_GuanChaZhe.php】观察者模式
<?php /** * [观察者模式] * PHP5中提供了 观察者(observer) 和 被观察者(subject) 的接口 * 在手册搜索:SplSubject (PHP 5 >= ...