首页 > 其他分享 >静态库封装之ComStr类

静态库封装之ComStr类

时间:2024-10-11 17:49:20浏览次数:8  
标签:封装 string 静态 unsigned long ComStr str 字符串 转换

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);
}

 

标签:封装,string,静态,unsigned,long,ComStr,str,字符串,转换
From: https://www.cnblogs.com/EricsT/p/18459006

相关文章

  • 模板和静态文件
    模板和静态文件1、templates模板2、静态文件2.1、static目录2.2、引用静态文件1、templates模板"templates"目录用于存放模板文件,通常是用于动态生成页面的文件。在app01目录下创建templates文件夹,html文件均保存在templates中在urls.py中添加路径后,在views.py......
  • 盘点免费且靠谱的AI大模型 API,统一封装,任性调用!
    ​现在做大模型,还有靠谱且免费的API接口吗?靠谱的不免费,免费的不靠谱,鱼和熊掌不可兼得?非也!对于简单的指令而言,绝大部分免费的LLMAPI还是能打的,本文就给大家介绍几款,猴哥亲测好用的免费的API接口!免费LLMAPI汇总(持续更新中)大模型免费版本免费限制备注API讯......
  • httpCLient请求工具类封装
    1、okhttp工具类依赖:<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><depe......
  • 汽车异构硬件平台开发如何进行静态代码分析
    随着汽车软件开发的复杂程度不断提升,尤其是智能网联汽车和自动驾驶技术的进步,汽车软件开发的复杂程度不断攀升。为了满足日益增长的功能需求和技术挑战,异构硬件平台被越来越多地采用,不同的工具链也不可避免地被引入到实际的开发流程中。这一趋势不仅增加了开发过程的技术多样性,也......
  • 【免费可视化仪表盘】轻松实现静态/动态数据可视化图表—积木仪表盘
    在当今信息爆炸的时代,如何从海量数据中快速提取关键信息,实现高效决策,成为了企业和个人面临的重大挑战。而积木仪表盘,就如同一位智慧的导航者,为你轻松开启数据可视化的精彩之旅。代码下载https://github.com/jeecgboot/JimuReport技术文档官方网站:http://jimureport.co......
  • Vue3 封装不定高虚拟列表 hooks
    //useVirtualList.tsimport{ref,onMounted,onBeforeUnmount,watch,nextTick}from"vue";importtype{Ref}from"vue";interfaceConfig{data:Ref<any[]>;//数据源scrollContainer:string;//滚动容器的元素选择器actualHeightC......
  • 组合数与自动取模类(已封装)
    usingi64=longlong;template<classT>constexprTpower(Ta,i64b){Tres=1;for(;b;b/=2,a*=a){if(b%2){res*=a;}}returnres;}constexpri64mul(i64a,i64b,i64p){i64re......
  • 【网络配置】聚合链路eNSP--静态 LACP 模式
            聚合链路(LinkAggregation),也称为链路捆绑、链路聚合组(LAG,LinkAggregationGroup)或以太网链路聚合,是一种将多个物理网络接口(例如以太网端口)组合成一个逻辑链路的技术。聚合链路的配置通常涉及以下两种模式:手工模式(StaticLAG):在这种模式下,网络管理员手动配置......
  • 7天毕设速成9小程序接口封装及对接
    1.接口封装1.1.请求接口封装新建文件夹request,新建index.js,封装接口接口调用对应此系列,springboot后端搭建笔记https://blog.csdn.net/PoofeeDong/article/details/140695913代码如下:constcommoneUrl="http://locahost:9999";//对应前面接口//get请求封装functi......
  • LED显示驱动/高亮数显屏驱动芯片VK16K33A 采用SOP28封装形式,可支持16SEGx8GRID的点阵L
    VK16K33A是一种带按键扫描接口的数码管或点阵LED驱动控制专用芯片,邱婷:188-2366-8825内部集成有数据锁存器、键盘扫描、LED驱动模块等电路。数据通过I2C通讯接口与MCU通信。SEG脚接LED阳极,GRID脚接LED阴极,可支持16SEGx8GRID的点阵LED显示面板。最大支持13×3的按键。内置上电......