C++技术之路:基础函数的应用

我们直接上代码,记录我平时用的函数。
1、将GB2312转化为UTF8

string TsGB2132ToUTF8(string  strSrc)
{
	string result;
	WCHAR *wstrSrc = NULL;
	char *szRes = NULL;
	int i;
	i = MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, NULL, 0);
	wstrSrc = new WCHAR[i + 1];
	MultiByteToWideChar(CP_ACP, 0, strSrc.c_str(), -1, wstrSrc, i);
	i = WideCharToMultiByte(CP_UTF8, 0, wstrSrc, -1, NULL, 0, NULL, NULL);
	szRes = new char[i + 1];
	WideCharToMultiByte(CP_UTF8, 0, wstrSrc, -1, szRes, i, NULL, NULL);
	result = string(szRes);
	if (wstrSrc != NULL)
	{
		delete[]wstrSrc;
		wstrSrc = NULL;
	}
	if (szRes != NULL)
	{
		delete[]szRes;
		szRes = NULL;
	}
	return result;
}

2、将UTF8转化为GB2312

string  TsUTF8ToGB2132(string  strSrc)
{
	string result;
	WCHAR *wstrSrc = NULL;
	char *szRes = NULL;
	int i;
	i = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0);
	wstrSrc = new WCHAR[i + 1];
	MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstrSrc, i);
	i = WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, NULL, 0, NULL, NULL);
	szRes = new char[i + 1];
	WideCharToMultiByte(CP_ACP, 0, wstrSrc, -1, szRes, i, NULL, NULL);
	result = string(szRes);
	if (wstrSrc != NULL)
	{
		delete[]wstrSrc;
		wstrSrc = NULL;
	}
	if (szRes != NULL)
	{
		delete[]szRes;
		szRes = NULL;
	}
	return result;
}

3、获取本地时间

unsigned int GetNowTime()
{
	static boost::posix_time::ptime  scBegin( boost::gregorian::date(1970, boost::gregorian::Jan, 1) );
	boost::posix_time::time_duration time_from_epoch = boost::posix_time::second_clock::universal_time()-scBegin;
	unsigned uSecond = time_from_epoch.total_seconds();
	return uSecond;
}
std::string GetTimeToStrYMD( unsigned iTime )
{
	boost::posix_time::ptime curtemp = usecond_to_boostptime( iTime );
	tm curtm      = boost::posix_time::to_tm( curtemp );
	tm* ptm       = &curtm;
	string strret = boost::str( boost::format("%d%02d%02d")%(ptm->tm_year+1900)%(ptm->tm_mon+1)%ptm->tm_mday );
	return strret;
}
string GetNowTimeToStrYMD_B()
{ 
	boost::posix_time::ptime curtemp( boost::posix_time::second_clock::local_time() );
	tm curtm      = boost::posix_time::to_tm( curtemp );
	tm* ptm       = &curtm;
	string strret = boost::str( boost::format("%d-%d-%d")%(ptm->tm_year+1900)%(ptm->tm_mon+1)%ptm->tm_mday );
	return strret;
}

4、类型转换

double strToDouble(const char *psource)
{
	if(!psource)
		return 0;
	string source = psource;
	try
	{
		if(source.empty())
			return 0;
		boost::replace_all(source," ","");

		if(source.empty())
			return 0;
		double ret = boost::lexical_cast<double>(source);
		return ret;
	} catch(...)
	{
		return 0;
	}
}
long long strToInt64No(const char *psource )
{
	iserror			= true;
	if( !psource )
		return 0;
	string source = psource;

	try
	{
		if( source.empty() )
			return 0;
		boost::replace_all(source," ","");

		if( source.empty() )
			return 0;

		long long ret = boost::lexical_cast<i64>( source );
		return ret;
	}
	catch (...)
	{
		return 0;
	}
}
string Int64ToStr( long longvvv )
{
	try
	{
		string strvv = boost::str( boost::format("%ll")%vvv );
		return strvv;
	}
	catch (...)
	{
		return "0";
	}
}
bool strcpyA(char *pDes,unsigned int umax,const char *pSour,unsigned int icalpos)
{
	if( (!pDes) || (!pSour) )
	{
		return false;
	}

	if( pDes == pSour )
	{
		return false;
	}

	if( umax>=strlen(pSour)+1 )
	{
		memset(pDes,0,umax);
		strncpy(pDes,pSour,strlen(pSour));
		return true;
	}
	return false;
}
vector<string> SpliteStrToVector(const std::string& sSource, char delim)
{
	std::string::size_type pos1, pos2;
	std::vector<std::string> strParams;
	pos2 = 0;
	while (pos2 != std::string::npos)
	{
		pos1 = sSource.find_first_not_of(delim, pos2);
		if (pos1 == std::string::npos)
			break;
		pos2 = sSource.find_first_of(delim, pos1 + 1);
		if (pos2 == std::string::npos)
		{
			if (pos1 != sSource.size())
				strParams.push_back(sSource.substr(pos1));
			break;
		}

		if( pos1!=pos2 )
			strParams.push_back(sSource.substr(pos1, pos2 - pos1));
		pos2 += 1;
	}
	return strParams;
}
string GetUUID()
{
	boost::uuids::uuid a_uuid = boost::uuids::random_generator()();
	const string tmp_uuid = boost::uuids::to_string(a_uuid);

	return tmp_uuid;
}
发布了11 篇原创文章 · 获赞 0 · 访问量 299

猜你喜欢

转载自blog.csdn.net/slyw77slyw/article/details/105281465
今日推荐