Location>code7788 >text

ComStr class for static library encapsulation

Popularity:684 ℃/2024-10-11 18:14:44


#pragma once

#include <string>
#include <vector>
using namespace std;

class ComStr
{
public:

	//CString
	//=============================================================================================================

	/*
	func:CString convert to string
	cs:pendingCString
	ret:convertedstring
	*/
	static string CStringToString(CString cs);

	/*
	func:CString convert to char[]
	cs:pendingCString
	ptrch:convertedchar[]
	*/
	static bool CStringTopchar(CString cs, char* ptrch);

	/*
	func:CString convert to PCHAR
	cs:pendingCString
	pch:convertedPCHAR
	*/
	static bool CStringToPCHAR(CString cs, PCHAR pch);

	/*
	func:string convert to CString
	str:pendingstring
	ret:convertedCString
	*/
	static CString stringToCString(string str);

	/*
	func:char[] convert to CString
	ptrch:pendingchar[]
	ret:convertedCString
	*/
	static CString pcharToCString(const char* ptrch);

	/*
	func:PCHAR convert to CString
	pch:pendingPCHAR
	ret:convertedCString
	*/
	static CString PCHARToCString(PCHAR pch);



	//string
	//=============================================================================================================

	/*
	func:String Splitting
	str:String to be split
	ch:split character
	ret:Segmented String Container
	*/
	static vector<string> stringSplit(string str, char ch);

	/*
	func:String Replacement for Specified String
	str:string to be replaced
	strOld:String to be replaced
	strNew:Specified String
	ret:Replacement of the completed string
	*/
	static string stringReplace(string str, string strOld, string strNew);



	//string to num [unsigned]
	//=============================================================================================================

	/*
	func:按照指定cardinal number将字符串convert tounsigned charinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static unsigned char stringToUChar(string str, int iBase);

	/*
	func:按照十进制将字符串convert tounsigned charinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned char stringToUCharDec(string str);

	/*
	func:按照十六进制将字符串convert tounsigned charinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned char stringToUCharHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tounsigned shortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static unsigned short stringToUShort(string str, int iBase);

	/*
	func:按照十进制将字符串convert tounsigned shortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned short stringToUShortDec(string str);

	/*
	func:按照十六进制将字符串convert tounsigned shortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned short stringToUShortHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tounsigned intinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static unsigned int stringToUInt(string str, int iBase);

	/*
	func:按照十进制将字符串convert tounsigned intinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned int stringToUIntDec(string str);

	/*
	func:按照十六进制将字符串convert tounsigned intinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned int stringToUIntHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tounsigned longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static unsigned long stringToULong(string str, int iBase);

	/*
	func:按照十进制将字符串convert tounsigned longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned long stringToULongDec(string str);

	/*
	func:按照十六进制将字符串convert tounsigned longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned long stringToULongHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tounsigned long longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static unsigned long long stringToULongLong(string str, int iBase);

	/*
	func:按照十进制将字符串convert tounsigned long longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned long long stringToULongLongDec(string str);

	/*
	func:按照十六进制将字符串convert tounsigned long longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static unsigned long long stringToULongLongHex(string str);



	//string to num [signed]
	//=============================================================================================================

	/*
	func:按照指定cardinal number将字符串convert tocharinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static char stringToChar(string str, int iBase);

	/*
	func:按照十进制将字符串convert tocharinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static char stringToCharDec(string str);

	/*
	func:按照十六进制将字符串convert tocharinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static char stringToCharHex(string str);

	/*
	func:按照指定cardinal number将字符串convert toshortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static short stringToShort(string str, int iBase);

	/*
	func:按照十进制将字符串convert toshortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static short stringToShortDec(string str);

	/*
	func:按照十六进制将字符串convert toshortinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static short stringToShortHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tointinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static int stringToInt(string str, int iBase);

	/*
	func:按照十进制将字符串convert tointinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static int stringToIntDec(string str);

	/*
	func:按照十六进制将字符串convert tointinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static int stringToIntHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tolonginteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static long stringToLong(string str, int iBase);

	/*
	func:按照十进制将字符串convert tolonginteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static long stringToLongDec(string str);

	/*
	func:按照十六进制将字符串convert tolonginteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static long stringToLongHex(string str);

	/*
	func:按照指定cardinal number将字符串convert tolong longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	iBase:cardinal number
	ret:converted数字
	*/
	static long long stringToLongLong(string str, int iBase);

	/*
	func:按照十进制将字符串convert tolong longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static long long stringToLongLongDec(string str);

	/*
	func:按照十六进制将字符串convert tolong longinteger (math.)[Failure to check legality,Defaults to string legal]
	str:pending字符串
	ret:converted数字
	*/
	static long long stringToLongLongHex(string str);
};

#include ""

string ComStr::CStringToString(CString cs)
{
	int len = ();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, cs, len + 1);
	if (pchSize == wstring::npos)
	{
		delete pch;
		return "";
	}

	string str(pch);
	delete pch;
	return str;
}

bool ComStr::CStringTopchar(CString cs, char * ptrch)
{
	int len = ();
	PCHAR pch = new char[len + 1];
	size_t pchSize = wcstombs(pch, cs, len + 1);
	if (pchSize == wstring::npos)
	{
		delete pch;
		return false;
	}

	string str(pch);
	delete pch;
	memccpy(ptrch, str.c_str(), sizeof(char), ());
	ptrch[()] = 0;
	return true;
}

bool ComStr::CStringToPCHAR(CString cs, PCHAR pch)
{
	int len = ();
	PCHAR pch_ = new char[len + 1];
	size_t pchSize = wcstombs(pch_, cs, len + 1);
	if (pchSize == wstring::npos)
	{
		delete pch_;
		return false;
	}

	string str(pch_);
	delete pch_;
	memccpy(pch, str.c_str(), sizeof(char), ());
	pch[()] = 0;
	return true;
}

CString ComStr::stringToCString(string str)
{
	return CString(str.c_str());
}

CString ComStr::pcharToCString(const char* pch)
{
	return CString(pch);
}

CString ComStr::PCHARToCString(PCHAR pch)
{
	return CString(pch);
}

vector<string> ComStr::stringSplit(string str, char ch)
{
	vector<string> StrVec;
	while (true)
	{
		int iPos = str.find_first_of(ch);

		if ( == iPos)
		{
			StrVec.push_back(str);
			break;
		}

		StrVec.push_back((0, iPos));
		str = (iPos + 1);
	}

	return StrVec;
}

string ComStr::stringReplace(string str, string strOld, string strNew)
{
	size_t iPos = (strOld, 0);
	while ( != iPos)
	{
		(iPos, (), strNew);
		iPos += ();
		iPos = (strOld, iPos);
	}

	return str;
}

unsigned char ComStr::stringToUChar(string str, int iBase)
{
	unsigned long ulRet = stoul(str, 0, iBase);
	unsigned char ucRet = ulRet;
	return ucRet;
}

unsigned char ComStr::stringToUCharDec(string str)
{
	unsigned long ulRet = stoul(str, 0, 10);
	unsigned char ucRet = ulRet;
	return ucRet;
}

unsigned char ComStr::stringToUCharHex(string str)
{
	unsigned long ulRet = stoul(str, 0, 16);
	unsigned char ucRet = ulRet;
	return ucRet;
}

unsigned short ComStr::stringToUShort(string str, int iBase)
{
	unsigned long ulRet = stoul(str, 0, iBase);
	unsigned short usRet = ulRet;
	return usRet;
}

unsigned short ComStr::stringToUShortDec(string str)
{
	unsigned long ulRet = stoul(str, 0, 10);
	unsigned short usRet = ulRet;
	return usRet;
}

unsigned short ComStr::stringToUShortHex(string str)
{
	unsigned long ulRet = stoul(str, 0, 16);
	unsigned short usRet = ulRet;
	return usRet;
}

unsigned int ComStr::stringToUInt(string str, int iBase)
{
	unsigned long ulRet = stoul(str, 0, iBase);
	unsigned int uiRet = ulRet;
	return uiRet;
}

unsigned int ComStr::stringToUIntDec(string str)
{
	unsigned long ulRet = stoul(str, 0, 10);
	unsigned int uiRet = ulRet;
	return uiRet;
}

unsigned int ComStr::stringToUIntHex(string str)
{
	unsigned long ulRet = stoul(str, 0, 16);
	unsigned int uiRet = ulRet;
	return uiRet;
}

unsigned long ComStr::stringToULong(string str, int iBase)
{
	return stoul(str, 0, iBase);
}

unsigned long ComStr::stringToULongDec(string str)
{
	return stoul(str, 0, 10);
}

unsigned long ComStr::stringToULongHex(string str)
{
	return stoul(str, 0, 16);
}

unsigned long long ComStr::stringToULongLong(string str, int iBase)
{
	return stoull(str, 0, iBase);
}

unsigned long long ComStr::stringToULongLongDec(string str)
{
	return stoull(str, 0, 10);
}

unsigned long long ComStr::stringToULongLongHex(string str)
{
	return stoull(str, 0, 16);
}

char ComStr::stringToChar(string str, int iBase)
{
	long lRet = stol(str, 0, iBase);
	char cRet = lRet;
	return cRet;
}

char ComStr::stringToCharDec(string str)
{
	long lRet = stol(str, 0, 10);
	char cRet = lRet;
	return cRet;
}

char ComStr::stringToCharHex(string str)
{
	long lRet = stol(str, 0, 16);
	char cRet = lRet;
	return cRet;
}

short ComStr::stringToShort(string str, int iBase)
{
	long lRet = stol(str, 0, iBase);
	short sRet = lRet;
	return sRet;
}

short ComStr::stringToShortDec(string str)
{
	long lRet = stol(str, 0, 10);
	short sRet = lRet;
	return sRet;
}

short ComStr::stringToShortHex(string str)
{
	long lRet = stol(str, 0, 16);
	short sRet = lRet;
	return sRet;
}

int ComStr::stringToInt(string str, int iBase)
{
	long lRet = stol(str, 0, iBase);
	int iRet = lRet;
	return iRet;
}

int ComStr::stringToIntDec(string str)
{
	long lRet = stol(str, 0, 10);
	int iRet = lRet;
	return iRet;
}

int ComStr::stringToIntHex(string str)
{
	long lRet = stol(str, 0, 16);
	int iRet = lRet;
	return iRet;
}

long ComStr::stringToLong(string str, int iBase)
{
	return stol(str, 0, iBase);
}

long ComStr::stringToLongDec(string str)
{
	return stol(str, 0, 10);
}

long ComStr::stringToLongHex(string str)
{
	return stol(str, 0, 16);
}

long long ComStr::stringToLongLong(string str, int iBase)
{
	return stoll(str, 0, iBase);
}

long long ComStr::stringToLongLongDec(string str)
{
	return stoll(str, 0, 10);
}

long long ComStr::stringToLongLongHex(string str)
{
	return stoll(str, 0, 16);
}