c++中char*\wchar_t*\string\wstring之间的相互转换

string U2A(const wstring& str)//Unicode字符转Ascii字符</span>
{
	string strDes;
	if ( str.empty() )
		goto __end;
	int nLen=::WideCharToMultiByte(CP_ACP, 0, str.c_str(), str.size(), NULL, 0, NULL, NULL);
	if ( 0==nLen )
		goto __end;
	char* pBuffer=new char[nLen+1];
	memset(pBuffer, 0, nLen+1);
	::WideCharToMultiByte(CP_ACP, 0, 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, 0, str.c_str(), str.size(), NULL, 0);
	if ( 0==nLen )
		goto __end;
	wchar_t* pBuffer=new wchar_t[nLen+1];
	memset(pBuffer, 0, nLen+1);
	::MultiByteToWideChar(CP_ACP, 0, 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, 0, wstrUnicode.c_str(), -1, NULL, 0, NULL, NULL);  
	char* pBuffer=new char[nLen+1];
	pBuffer[nLen] = '\0';
	nLen = WideCharToMultiByte(CP_UTF8, 0, wstrUnicode.c_str(), -1, 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, 0);
	wchar_t* wstrBuf = new wchar_t[u16Len + 1];
	::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 = 0;
	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=0; 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>>4) );
			strDes+=ToHex( ch%16 );
		}
	}
	return strDes;
}
//URL解码
string UrlDecode(const string& strSrc)
{
	string strDes;  
	for ( size_t i = 0; 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<<4) + l;  
		}  
		else strDes += ch;  
	}  
	return strDes; 
}
//替换字符串
wstring StrReplaceW(const wstring& strContent, const wstring& strTag, const wstring& strReplace)
{
	size_t nBegin=0, nFind=0;
	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=0, nFind=0;
	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;
}

       以前的一份是很原始的代码,在多线程中已经不适用。注意:如果是utf8字符转ascii字符,这里没有单独函数,你需要两步来实现,utf8--->Unicode--->ascii,反之一样。

       至于编码的区别,简单地说下。Unicode编码中一个字符占两个字节,一个中文字占用两字节;Ascii编码是比较原始的编码方式,一个字符占用一个字节,一个中文字占用两字节;utf-8编码也叫变长编码,而前面两种编码长度是固定了的,utf-8编码中一个汉字可能占两个字节或者三个、四个字节。

       还有一点要说明的是,英文字母在这三种编码中的区别,以字母‘a’为例,Unicode编码中两个个字节是0x00 0x61;ascii编码中一个字节是0x61;UTF-8编码中也是一个字节0x61。其实,英文字符在ascii和uft-8编码中完全一样,所以在实际开发中,如果你需要在ascii和utf-8编码间转码而又知道里面全是英文,请不要转码!

      除了转码的几个基本函数,还附带了URL编码、解码和字符串替换的函数,这些代码都是在实际项目中使用的,应该很稳定,欢迎指正。
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值