静态库封装之ComStr类

ComStr.h


#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

#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)