📄 tools.cpp
字号:
// TOOLS.cpp: implementation of the TOOLS class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CREATOR.h"
#include "TOOLS.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
TOOLS::TOOLS()
{
}
TOOLS::~TOOLS()
{
}
CString TOOLS::Drawword(CString string, int iCount)//读string的第iCount个单词
{CString copystring,restring;
int j=0,number=0;//记录数到第几个单词了
copystring=' '+string+' ';
if(iCount==0) return "";
for(int i=0;i<copystring.GetLength();i++)
{
if(IsTheFirstCharofTheWord(copystring,i))
{
number++;
if(number==iCount)
{
do
{
restring+=copystring.GetAt(i);
}while(!IsTheEndCharofTheWord(copystring,i++));
return restring;
}
}
}
return "";
}
BOOL TOOLS::IsWordChar(char ch)
{if(isalpha(ch)||isdigit(ch)||ch=='_') return TRUE;
else return FALSE;
}
BOOL TOOLS::IsTheFirstCharofTheWord(CString string, int n)//string的第n个字符是否为单词的首字母
{ if(n>=string.GetLength()) return FALSE;
CString copystring;
char ch1,ch2;
ch1=string.GetAt(n);
copystring=' '+string+' ';
ch2=copystring.GetAt(n+1);
if(!IsWordChar(copystring.GetAt(n))&&IsWordChar(copystring.GetAt(n+1))) return TRUE;
else return FALSE;
}
BOOL TOOLS::IsTheEndCharofTheWord(CString string,int n)
{
char ch1,ch2;
if(n>string.GetLength()) return FALSE;
CString copystring;
copystring=' '+string+' ';
ch1=copystring.GetAt(n+1);
ch2=copystring.GetAt(n+2);
if(IsWordChar(copystring.GetAt(n+1))&&!IsWordChar(copystring.GetAt(n+2))) return TRUE;
else return FALSE;
}
void TOOLS::InsertString(CString &string, int iCount, CString insertstring)
//在第icount个字符后插入字符串insertstring
{string=string.Left(iCount)+insertstring+string.Mid(iCount);
}
BOOL TOOLS::IsDigit(CString string)//允许有前置空格
{ BOOL flag=TRUE;
string=Drawword(string,1);
if(string.GetLength()==0) return FALSE;
for(int i=0;i<string.GetLength();i++)
if(!isdigit(string.GetAt(i))) flag=FALSE;
return flag;
}
BOOL TOOLS::IsXDigit(CString string)//前面必须标明是"0X",允许有前置空格
{ BOOL flag=TRUE;
string=Drawword(string,1);
if(string.GetLength()==0) return FALSE;
if(string.Left(2)=="0X"||string.Left(2)=="0x")
{string=string.Mid(2);
for(int i=0;i<string.GetLength();i++)
if(!isxdigit(string.GetAt(i))) flag=FALSE;
}
else return FALSE;
return flag;
}
CString TOOLS::DtoXS(int Data, int n)//将Data转换为n个字符长的16进制字符串,不足补0,超过不管
{CString restring;
restring.Format("%x",Data);
while(restring.GetLength()<n)
{restring="0"+restring;
}
return "0x"+restring;
}
int TOOLS::XStoD(CString string)//将16进制字符串转换为十进制数,可识别"0x",也可不带它
{int i=0,sum=0;
string=Drawword(string,1);
if(string.Left(2)=="0X"||string.Left(2)=="0x") string=string.Mid(2);
while( i<string.GetLength())
{sum*=16;
sum+=CtoXD(string.GetAt(i++));
}
return sum;
}
int TOOLS::CtoXD(char ch)//返回字符对应的十进制数
{if(isxdigit(ch))
if(isdigit(ch)) return ch-'0';
else return toupper(ch)-'A'+10;
return 0;
}
CString TOOLS::OutData(CString string)//从"SB[0000]"剥离数据出来;只能剥离第一个数据
{CString restring;
BOOL flag=FALSE;
int i=0;
while(i<string.GetLength())
{ if(string.GetAt(i)=='[') flag=TRUE;
if(string.GetAt(i)==']')
{
if( restring.GetAt(1)=='-')
{
restring=Minus("10000",restring.Mid(2));
}
return restring;
}
if(flag) restring+=string.GetAt(i);
i++;
}
return restring;
}
CString TOOLS::KeepLengthString(CString string,int length)//让字符串保持length长,不足的前面补零
{
while(string.GetLength()<length)
{string='0'+string;
};
return string;
}
int TOOLS::DStoX(CString string)//将一个十进制字符串转换为16进制字数
{int sum=0;
string=Drawword(string,1);
for(int i=0;i<string.GetLength();i++)
{sum*=10;
sum+=string.GetAt(i)-'0';
}
return sum;
}
CString TOOLS::DtoB(int Data)//将Data转换为2进制串
{CString restring,temp;
int i=0;
while(Data!=0)
{temp.Format("%d",Data%2);
restring+=temp;
Data/=2;
}
restring.MakeReverse();
return restring;
}
CString TOOLS::XStoB(CString string)//将十六进制串转换为二进制串
{ return DtoB(XStoD(string));
}
CString TOOLS::DStoB(CString string)
{ return DtoB(atoi(string));
}
CString TOOLS::BtoX(CString sB)
{ CString restring,temp;;
int sum=0;
char ch=0;
for(int i = 0; i < sB.GetLength() / 8 ;i++)
{
sum=0;
for(int j = 0; j < 8; j++)
{
sum*=2;
ch = sB.GetAt(8*i + j);
if(ch == '1')
sum += 1;
}
temp.Format("0x%.2x," , sum);
restring+=temp;
}
return restring;
}
CString TOOLS::Minus(CString string1, CString string2)//两个十六进制字符串的减法
{
CString restring;
string1.MakeUpper();
string2.MakeUpper();
restring.Format("%x",XStoD(string1)-XStoD(string2));
return restring;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -