c++ 常用的一些字符转换
来源:互联网 发布:b2b平台如运营 知乎 编辑:程序博客网 时间:2024/05/17 03:43
wstring string_to_wstring( string& s ){ if( s.empty() ) return _T(""); int retval = ::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),0,0); wstring re; re.resize( retval ); ::MultiByteToWideChar(CP_ACP,0,s.c_str(),s.size(),&re[0],re.size()); return re;}/// 宽字符串 到 一般窄字符串string wstring_to_string( wstring& ws ){ if( ws.empty() ) return ""; int retval = ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),0,0,0,0); string re; re.resize( retval ); ::WideCharToMultiByte(CP_ACP,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0); return re;}/// 宽字符串 到 一般窄字符串string wstring_to_string( wchar_t* ws, size_t sz ){ if( ws == NULL || sz == 0 ) return ""; int retval = ::WideCharToMultiByte(CP_ACP,0,ws,sz,0,0,0,0); string re; re.resize( retval ); ::WideCharToMultiByte(CP_ACP,0,ws,sz,&re[0],re.size(),0,0); return re;}/// 窄字符串 到 宽字符串wstring utf8_to_wstring( string& s ){ if( s.empty() ) return _T(""); int retval = ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),0,0); wstring re; re.resize( retval ); ::MultiByteToWideChar(CP_UTF8,0,s.c_str(),s.size(),&re[0],re.size()); return re;}/// 宽字符串 到 UTF8string wstring_to_utf8( wstring& ws ){ if( ws.empty() ) return ""; int retval = ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),0,0,0,0); string re; re.resize( retval ); ::WideCharToMultiByte(CP_UTF8,0,ws.c_str(),ws.size(),&re[0],re.size(),0,0); return re;}string wstring_to_utf8( const wchar_t* ws, size_t sz ){ if( ws == NULL || sz == 0 ) return ""; int retval = ::WideCharToMultiByte(CP_UTF8,0,ws,sz,0,0,0,0); string re; re.resize( retval ); ::WideCharToMultiByte(CP_UTF8,0,ws,sz,&re[0],re.size(),0,0); return re;}string from_base64(string &base64){ int nSize = ATL::Base64DecodeGetRequiredLength(base64.size()); byte* pData = new byte[nSize+1]; ATL::Base64Decode(base64.c_str(), base64.size(), pData, &nSize); pData[nSize] = 0; string str = (char *)pData; delete []pData; return wstring_to_string(utf8_to_wstring(str));}wstring utf8_to_unicode(const string& utf8string){ int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0); if (widesize == ERROR_NO_UNICODE_TRANSLATION) { throw exception("Invalid UTF-8 sequence."); } if (widesize == 0) { throw exception("Error in conversion."); } vector<wchar_t> resultstring(widesize); int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize); if (convresult != widesize) { throw exception("La falla!"); } return wstring(&resultstring[0]);}wstring acsi_to_widebyte(string& strascii){ int widesize = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0); if (widesize == ERROR_NO_UNICODE_TRANSLATION) { throw exception("Invalid UTF-8 sequence."); } if (widesize == 0) { throw exception("Error in conversion."); } vector<wchar_t> resultstring(widesize); int convresult = MultiByteToWideChar (CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize); if (convresult != widesize) { throw exception("La falla!"); } return wstring(&resultstring[0]); } string unicode_to_utf8(const wstring& widestring) { int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL); if (utf8size == 0) { throw exception("Error in conversion."); } vector<char> resultstring(utf8size); int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL); if (convresult != utf8size) { throw exception("La falla!"); } return string(&resultstring[0]);}string ascii_to_utf8(string& strAsciiCode){ string strRet(""); //先把 ascii 转为 unicode wstring wstr = acsi_to_widebyte(strAsciiCode); //最后把 unicode 转为 utf8 strRet = unicode_to_utf8(wstr); return strRet;}int split( const string& str,vector<string>& subs, const char& sep /* = */ ){ string temp = str; temp += sep; int count = 0; size_t pos = temp.find( sep ); size_t from = 0; while ( pos != temp.npos ) { string sub_str = temp.substr( from, pos - from ); //if ( sub_str.length() > 0 ) { ++ count; subs.push_back( sub_str ); } from = pos + 1; pos = temp.find( sep, from ); } return count;}int splitw( const wstring& str, vector<wstring>& subs, const wchar_t& sep /* = */ ){ wstring temp = str; temp += sep; int count = 0; size_t pos = temp.find( sep ); size_t from = 0; while ( pos != temp.npos ) { wstring sub_str = temp.substr( from, pos - from ); //if ( sub_str.length() > 0 ) { ++ count; subs.push_back( sub_str ); } from = pos + 1; pos = temp.find( sep, from ); } return count;}///< 字符串替换string& replace_all_s(string& str, const string& old_value,const string& new_value){ while(true) { string::size_type pos(0); if( (pos = str.find( old_value ) )!=string::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str;}wstring& replace_all_ws(wstring& str, const wstring& old_value,const wstring& new_value){ while(true) { wstring::size_type pos(0); if( (pos = str.find( old_value ) )!=wstring::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str;}string& replace_all_distinct_s(string& str,const string& old_value,const string& new_value){ for(string::size_type pos(0); pos!=string::npos; pos+=new_value.length()) { if( (pos=str.find(old_value,pos))!=string::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str;}wstring& replace_all_distinct_ws(wstring& str,const wstring& old_value,const wstring& new_value){ for(wstring::size_type pos(0); pos!=wstring::npos; pos+=new_value.length() ) { if( (pos=str.find(old_value,pos))!=wstring::npos ) str.replace(pos,old_value.length(),new_value); else break; } return str;}
1 0
- 一些常用的字符转换
- c++ 常用的一些字符转换
- Windows编程中一些常用的字符(串)类型转换
- C常用字符转换函数
- 关于字符的一些转换
- 一些QT的字符转换
- c语言一些常用的字符串转换函数
- 常用的字符转换类
- Linux 常用C函数(字符转换篇)
- Linux 常用C函数(字符转换篇)
- C常用的字符处理
- XML一些字符的转换设置
- 常用的一些字符了,什么的使用。
- 一些常用的字符操作函数
- 常用的js,字符截取,时间转换
- 常用的字符编码转换集合
- 【C#】常用的一些特性
- html常用字符转换
- 自然语言处理(NLP)在企业应用中的实践(二)
- Spark.MLlib之线性分类模型学习笔记
- [BZOJ1758] [WC2010] 重建计划 - 树分治
- nginx之红黑树
- Netty4.0学习笔记系列之一:Server与Client的通讯
- c++ 常用的一些字符转换
- 八大排序算法
- Netty4.0学习笔记系列之二:Handler的执行顺序
- 正则表达式JAVA254-262
- Netty4.0学习笔记系列之三:构建简单的http服务
- Perl 正则表达式元字符
- Netty4.0学习笔记系列之四:混合使用coder和handler
- Netty4.0学习笔记系列之五:自定义通讯协议
- Java反射获取类和对象信息全解析