靜態庫封裝之ComStr類

EricsT發表於2024-10-11

ComStr.h


/*
@author:EricsT
@data:20241008
@version:V1.3
@history:
	@author @data @version @content
	EricsT 20241008 V1.0 新增ComStr類[CString部分]
	EricsT 20241009 V1.1 新增string部分
	EricsT 20241010 V1.2 新增string to num[unsigned]部分
	EricsT 20241011 V1.3 新增string to num[signed]部分
*/

#pragma once

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

class ComStr
{
public:

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

	/*
	func:CString 轉換為 string
	cs:待轉換的CString
	ret:轉換後的string
	*/
	static string CStringToString(CString cs);

	/*
	func:CString 轉換為 char[]
	cs:待轉換的CString
	ptrch:轉換後的char[]
	*/
	static bool CStringTopchar(CString cs, char* ptrch);

	/*
	func:CString 轉換為 PCHAR
	cs:待轉換的CString
	pch:轉換後的PCHAR
	*/
	static bool CStringToPCHAR(CString cs, PCHAR pch);

	/*
	func:string 轉換為 CString
	str:待轉換的string
	ret:轉換後的CString
	*/
	static CString stringToCString(string str);

	/*
	func:char[] 轉換為 CString
	ptrch:待轉換的char[]
	ret:轉換後的CString
	*/
	static CString pcharToCString(const char* ptrch);

	/*
	func:PCHAR 轉換為 CString
	pch:待轉換的PCHAR
	ret:轉換後的CString
	*/
	static CString PCHARToCString(PCHAR pch);



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

	/*
	func:字串分割
	str:待分割的字串
	ch:分割字元
	ret:分割完成的字串容器
	*/
	static vector<string> stringSplit(string str, char ch);

	/*
	func:字串替換指定字串
	str:待替換的的字串
	strOld:需要被替換的字串
	strNew:指定的字串
	ret:替換完成的字串
	*/
	static string stringReplace(string str, string strOld, string strNew);



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

	/*
	func:按照指定基數將字串轉換為unsigned char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static unsigned char stringToUChar(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為unsigned char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned char stringToUCharDec(string str);

	/*
	func:按照十六進位制將字串轉換為unsigned char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned char stringToUCharHex(string str);

	/*
	func:按照指定基數將字串轉換為unsigned short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static unsigned short stringToUShort(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為unsigned short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned short stringToUShortDec(string str);

	/*
	func:按照十六進位制將字串轉換為unsigned short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned short stringToUShortHex(string str);

	/*
	func:按照指定基數將字串轉換為unsigned int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static unsigned int stringToUInt(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為unsigned int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned int stringToUIntDec(string str);

	/*
	func:按照十六進位制將字串轉換為unsigned int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned int stringToUIntHex(string str);

	/*
	func:按照指定基數將字串轉換為unsigned long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static unsigned long stringToULong(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為unsigned long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned long stringToULongDec(string str);

	/*
	func:按照十六進位制將字串轉換為unsigned long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned long stringToULongHex(string str);

	/*
	func:按照指定基數將字串轉換為unsigned long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static unsigned long long stringToULongLong(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為unsigned long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned long long stringToULongLongDec(string str);

	/*
	func:按照十六進位制將字串轉換為unsigned long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static unsigned long long stringToULongLongHex(string str);



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

	/*
	func:按照指定基數將字串轉換為char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static char stringToChar(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static char stringToCharDec(string str);

	/*
	func:按照十六進位制將字串轉換為char型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static char stringToCharHex(string str);

	/*
	func:按照指定基數將字串轉換為short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static short stringToShort(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static short stringToShortDec(string str);

	/*
	func:按照十六進位制將字串轉換為short型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static short stringToShortHex(string str);

	/*
	func:按照指定基數將字串轉換為int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static int stringToInt(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static int stringToIntDec(string str);

	/*
	func:按照十六進位制將字串轉換為int型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static int stringToIntHex(string str);

	/*
	func:按照指定基數將字串轉換為long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static long stringToLong(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static long stringToLongDec(string str);

	/*
	func:按照十六進位制將字串轉換為long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static long stringToLongHex(string str);

	/*
	func:按照指定基數將字串轉換為long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	iBase:基數
	ret:轉換後的數字
	*/
	static long long stringToLongLong(string str, int iBase);

	/*
	func:按照十進位制將字串轉換為long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static long long stringToLongLongDec(string str);

	/*
	func:按照十六進位制將字串轉換為long long型整數[不檢查合法性,預設為字串合法]
	str:待轉換的字串
	ret:轉換後的數字
	*/
	static long long stringToLongLongHex(string str);
};

ComStr.cpp


/*
@author:EricsT
@data:20241008
@version:V1.3
@history:
	@author @data @version @content
	EricsT 20241008 V1.0 新增ComStr類[CString部分]
	EricsT 20241009 V1.1 新增string部分
	EricsT 20241010 V1.2 新增string to num[unsigned]部分
	EricsT 20241011 V1.3 新增string to num[signed]部分
*/

#include "stdafx.h"
#include "ComStr.h"

string ComStr::CStringToString(CString cs)
{
	int len = cs.GetLength();
	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 = cs.GetLength();
	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), str.size());
	ptrch[str.size()] = 0;
	return true;
}

bool ComStr::CStringToPCHAR(CString cs, PCHAR pch)
{
	int len = cs.GetLength();
	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), str.size());
	pch[str.size()] = 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 (str.npos == iPos)
		{
			StrVec.push_back(str);
			break;
		}

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

	return StrVec;
}

string ComStr::stringReplace(string str, string strOld, string strNew)
{
	size_t iPos = str.find(strOld, 0);
	while (str.npos != iPos)
	{
		str.replace(iPos, strOld.length(), strNew);
		iPos += strNew.length();
		iPos = str.find(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);
}

在VS編譯器內會報C4996錯誤,解決見下文:

C4996 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. - EricsT - 部落格園 (cnblogs.com)

相關文章