静态库封装之ComStr类
ComStr.h
/*
@author:EricsT
@data:20241008
@version:V1.5
@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]部分
EricsT 20241018 V1.4 修改string部分[新增 string case operation]
EricsT 20241024 V1.5 修改部分函数说明注释
*/
#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);//20241008
/*
func:CString 转换为 char[]
@cs:待转换的CString
@ptrch:转换后的char[]
ret:成功true,失败false
*/
static bool CStringTopchar(CString cs, char* ptrch);//20241008
/*
func:CString 转换为 PCHAR
@cs:待转换的CString
@pch:转换后的PCHAR
ret:成功true,失败false
*/
static bool CStringToPCHAR(CString cs, PCHAR pch);//20241008
/*
func:string 转换为 CString
@str:待转换的string
ret:转换后的CString
*/
static CString stringToCString(string str);//20241008
/*
func:char[] 转换为 CString
@ptrch:待转换的char[]
ret:转换后的CString
*/
static CString pcharToCString(const char* ptrch);//20241008
/*
func:PCHAR 转换为 CString
@pch:待转换的PCHAR
ret:转换后的CString
*/
static CString PCHARToCString(PCHAR pch);//20241008
//string
//=============================================================================================================
/*
func:字符串分割
@str:待分割的字符串
@ch:分割字符
ret:分割完成的字符串容器
*/
static vector<string> stringSplit(string str, char ch);//20241009
/*
func:字符串替换指定字符串
@str:待替换的的字符串
@strOld:需要被替换的字符串
@strNew:指定的字符串
ret:替换完成的字符串
*/
static string stringReplace(string str, string strOld, string strNew);//20241009
/*
func:字符串转换为大写
@str:待转换的的字符串
ret:转换完成的字符串
*/
static string stringToUpper(string str);//20241018
/*
func:字符串转换为小写
@str:待转换的的字符串
ret:转换完成的字符串
*/
static string stringToLower(string str);//20241018
//string to num [unsigned]
//=============================================================================================================
/*
func:按照指定基数将字符串转换为unsigned char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static unsigned char stringToUChar(string str, int iBase);//20241010
/*
func:按照十进制将字符串转换为unsigned char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned char stringToUCharDec(string str);//20241010
/*
func:按照十六进制将字符串转换为unsigned char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned char stringToUCharHex(string str);//20241010
/*
func:按照指定基数将字符串转换为unsigned short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static unsigned short stringToUShort(string str, int iBase);//20241010
/*
func:按照十进制将字符串转换为unsigned short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned short stringToUShortDec(string str);//20241010
/*
func:按照十六进制将字符串转换为unsigned short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned short stringToUShortHex(string str);//20241010
/*
func:按照指定基数将字符串转换为unsigned int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static unsigned int stringToUInt(string str, int iBase);//20241010
/*
func:按照十进制将字符串转换为unsigned int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned int stringToUIntDec(string str);//20241010
/*
func:按照十六进制将字符串转换为unsigned int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned int stringToUIntHex(string str);//20241010
/*
func:按照指定基数将字符串转换为unsigned long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static unsigned long stringToULong(string str, int iBase);//20241010
/*
func:按照十进制将字符串转换为unsigned long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned long stringToULongDec(string str);//20241010
/*
func:按照十六进制将字符串转换为unsigned long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned long stringToULongHex(string str);//20241010
/*
func:按照指定基数将字符串转换为unsigned long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static unsigned long long stringToULongLong(string str, int iBase);//20241010
/*
func:按照十进制将字符串转换为unsigned long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned long long stringToULongLongDec(string str);//20241010
/*
func:按照十六进制将字符串转换为unsigned long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static unsigned long long stringToULongLongHex(string str);//20241010
//string to num [signed]
//=============================================================================================================
/*
func:按照指定基数将字符串转换为char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static char stringToChar(string str, int iBase);//20241011
/*
func:按照十进制将字符串转换为char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static char stringToCharDec(string str);//20241011
/*
func:按照十六进制将字符串转换为char型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static char stringToCharHex(string str);//20241011
/*
func:按照指定基数将字符串转换为short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static short stringToShort(string str, int iBase);//20241011
/*
func:按照十进制将字符串转换为short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static short stringToShortDec(string str);//20241011
/*
func:按照十六进制将字符串转换为short型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static short stringToShortHex(string str);//20241011
/*
func:按照指定基数将字符串转换为int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static int stringToInt(string str, int iBase);//20241011
/*
func:按照十进制将字符串转换为int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static int stringToIntDec(string str);//20241011
/*
func:按照十六进制将字符串转换为int型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static int stringToIntHex(string str);//20241011
/*
func:按照指定基数将字符串转换为long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static long stringToLong(string str, int iBase);//20241011
/*
func:按照十进制将字符串转换为long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static long stringToLongDec(string str);//20241011
/*
func:按照十六进制将字符串转换为long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static long stringToLongHex(string str);//20241011
/*
func:按照指定基数将字符串转换为long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
@iBase:基数
ret:转换后的数字
*/
static long long stringToLongLong(string str, int iBase);//20241011
/*
func:按照十进制将字符串转换为long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static long long stringToLongLongDec(string str);//20241011
/*
func:按照十六进制将字符串转换为long long型整数[不检查合法性,默认为字符串合法]
@str:待转换的字符串
ret:转换后的数字
*/
static long long stringToLongLongHex(string str);//20241011
};
ComStr.cpp
/*
@author:EricsT
@data:20241008
@version:V1.5
*/
#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;
}
string ComStr::stringToUpper(string str)
{
for (int i = 0; i < str.size(); i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
}
return str;
}
string ComStr::stringToLower(string str)
{
for (int i = 0; i < str.size(); i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
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错误,解决见下文:
本文来自博客园,作者:EricsT,转载请注明原文链接:https://www.cnblogs.com/EricsT/p/18459006