📄 umath_class.pas
字号:
unit uMath_Class;
interface
uses
SysUtils;
Const
mtNone =$00;
mtSystem =$01;
mtCard =$02;
mtApplication=$03;
mtTransmit =$04; //通讯错误
mtCancel =$05;
mtDB =$06;
mtTACErr =$07;
mtMACErr =$08;
mtLenErr =$09;
mtUnPackErr =$0A;
mtIllegalConnect=$0B;
mtPkey =$0C;
csCoName='中国石油天然气股份有限公司';// csCopyright='中国计算机软件与技术服务总公司';
Type
TBitStr=String[1];
TByteStr=String[2];
T2ByteStr=String[4];
T3ByteStr=String[6];
T4ByteStr=String[8];
T5ByteStr=String[10];
T6ByteStr=String[12];//tid
T7ByteStr=String[14];//datetime
T8ByteStr=String[16];
T10ByteStr=String[20];
T16ByteStr=String[32];
T20ByteStr=String[40];
TErrMsgRec=Record //错误记录结构
MsgType:Byte;//错误类型
ErrMsg:String; //错误信息
ErrCode:String;
End;
TBaseMath=Class(TObject)
protected
FErrMsg:TErrMsgRec;
Function fniChar_To_Num(Ic_Char:char):Byte;
Function fnbHex_To_Number(Is_Str:string;Var OResult:Word):Boolean; overload;
Function fnbHex_To_Number(Is_Str:string;Var OResult:Integer):Boolean; overload;
Function fnbHex_To_Number(Is_Str:string;Var OResult:LongWord):Boolean;overload;
Function fnbHex_To_Number(Is_Str:string;Var OResult:int64):Boolean; overload;
Function fnsAdd_No(IsString:String):string;
Function fnsStr1_Xor_Str2(IsString1,IsString2:string):string; //Bin
Function fnsNot_Str(IsString:string):string; //Bin
Function fnStrToDate(IsData_Str:String):TDateTime; //yyyymmdd
Function fnStrToTime(IsData_Str:String):TDateTime; //hhmmss
Function fnStrToDateTime(IsData_Str:String):TDateTime; //yyyymmddhhmmss
Function ByteCircleShL(IiData,IiBitNum:Byte):Byte;
Function ByteCircleShR(IiData,IiBitNum:Byte):Byte;
Procedure pClearErr;
Public
Constructor create; virtual;
Procedure free; virtual;
Function fnsBin_To_Hex(Is_Str:String):String;
Function fnsHex_To_Bin(Is_Str:String):String;
Function fnsGetRandomStr(IiRandomStrLen:Integer):String;
Function fnsDecToBin(aValue: LongInt): String;
Function fnLBinToDec(aValue: String ): LongInt;
Function fnsAdd_F(Is_Str:String):String;
Function fnsClear_F(Is_Str:String):String;
Property Err:TErrMsgRec Read FErrMsg;
End;
TMath=Class(TBaseMath)
Public
Function fnHexToInt(IsData:String):Integer;
Function fnHexToWord(IsData:String):Word;
Function fnHexToLongWord(IsData:String):LongWord;
Function fnHexToInt64(IsData:String):Int64;
Function fnStrToDateTime(IsData_Str:String):TDateTime;
Function fnsAdd_F(Is_Str:String):String;
Function fnsClear_F(Is_Str:String):String;
End;
Function fnsGetErrType(Err:TErrMsgRec):String;
Function fniChToByte(IcChar:Char):Byte;
//Function fnsGetShowErrInfo(Err:TErrMsgRec;Var OsTitle,OsMsg:String):Boolean;
implementation
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function fniChToByte(IcChar:Char):Byte;
var
Li_Re:Byte;
begin
case IcChar of
'0'..'9': Li_Re:=Ord(IcChar)-ord('0'); //'0' is 32
'A'..'Z': Li_Re:=Ord(IcChar)-ord('A')+10;
'a'..'z': Li_Re:=Ord(IcChar)-ord('a')+10;
else
Li_Re:=16;
end ;
Result:=Li_Re;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Constructor TBaseMath.create;
Begin
End;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Procedure TBaseMath.free;
Begin
End;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnsNot_Str(IsString:string):string;
var
Li_Count:integer;
Ls_Temp:string;
Begin
SetLength(Ls_Temp,length(IsString));
for Li_Count:=1 to length(IsString) do
Ls_Temp[Li_Count]:=chr(not ord(IsString[Li_Count]));
Result:=Ls_Temp;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnsStr1_Xor_Str2(IsString1,IsString2:string):string;
var
Li_Count:integer;
Ls_Temp:string;
Begin
Try
If Length(IsString1)>Length(IsString2) then
SetLength(Ls_Temp,length(IsString2))
Else
SetLength(Ls_Temp,length(IsString1));
for Li_Count:=1 to length(IsString1) do
Ls_Temp[Li_Count]:=chr(ord(IsString1[Li_Count]) xor ord(IsString2[Li_Count]));
Result:=Ls_Temp;
Except Result:='';End;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fniChar_To_Num(Ic_Char:char):Byte;
//'0..9'->0..9,'A..F,a..f'->10..16
var
Li_Re:Byte;
begin
case Ic_Char of
'0'..'9': Li_Re:=Ord(Ic_Char)-ord('0'); //'0' is 32
'A'..'Z': Li_Re:=Ord(Ic_Char)-ord('A')+10;
'a'..'z': Li_Re:=Ord(Ic_Char)-ord('a')+10;
else
Li_Re:=16;
end ;
Result:=Li_Re;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnsBin_To_Hex(Is_Str:String):String;
Var
Li_Count,Li_Str_Len:integer;
Ls_Temp_Str:String;
Begin
Try
Li_Str_Len:=length(Is_Str);
for Li_Count:=1 to Li_Str_Len do
Ls_Temp_Str:=Ls_Temp_Str+IntToHex(ord(Is_Str[Li_Count]),2);
Result:=Ls_Temp_Str;
Except On E:Exception Do
Result:='';
End;
End;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnsHex_To_Bin(Is_Str:String):String;
Var
LiCount,Li_Str_Len:integer;
Ls_Temp_Str:String;
Li_Temp_int1,Li_Temp_int2:byte;
Begin
Try
Li_Str_Len:=length(Is_Str) div 2;
SetLength(Ls_Temp_Str,Li_Str_Len);
For LiCount:=1 To Li_Str_Len DO
Begin
Li_Temp_int1:=fniChar_To_Num(Is_Str[LiCount*2-1]);
Li_Temp_int2:=fniChar_To_Num(Is_Str[LiCount*2]);
If (Li_temp_int1=16) or (Li_temp_int2=16) then //源数据错误
begin
Result:='';
exit;
end;
Ls_Temp_Str[LiCount]:=char(Li_temp_int1*16+Li_temp_int2)
End;
Result:=Ls_Temp_Str;
Except On E:Exception Do
Result:='';
End;
End;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnbHex_To_Number(Is_Str:string;Var OResult:Integer):Boolean;
var
Li_count,Li_len:Integer;
Begin
li_len:=length(Is_str);
if li_len>8 then
Begin
Result:=False;
exit;
end;
OResult:=0;
for Li_count:=1 to li_len do
OResult:=OResult*16+fniChar_To_Num(Is_str[Li_count]);
Result:=True;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnbHex_To_Number(Is_Str:string;Var OResult:Word):Boolean;
var
Li_count,Li_len:Word;
Begin
li_len:=length(Is_str);
if li_len>8 then
Begin
Result:=False;
exit;
end;
OResult:=0;
for Li_count:=1 to li_len do
OResult:=OResult*16+fniChar_To_Num(Is_str[Li_count]);
Result:=True;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnbHex_To_Number(Is_Str:string;Var OResult:longWord):Boolean;
var
Li_count,Li_len:integer;
Begin
li_len:=length(Is_str);
if li_len>8 then
Begin
Result:=False;
exit;
end;
OResult:=0;
for Li_count:=1 to li_len do
OResult:=OResult*16+fniChar_To_Num(Is_str[Li_count]);
Result:=True;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnbHex_To_Number(Is_Str:string;Var OResult:int64):Boolean;
var
Li_count,Li_len:integer;
Begin
li_len:=length(Is_str);
if li_len>16 then
Begin
Result:=False;
exit;
end;
OResult:=0;
for Li_count:=1 to li_len do
OResult:=OResult*16+fniChar_To_Num(Is_str[Li_count]);
Result:=True;
end;
//---------------------------------------------------------------------------------//
//
//---------------------------------------------------------------------------------//
Function TBaseMath.fnsAdd_F(Is_Str:String):String;
Var i:Integer;
Temp:String;
Begin
Temp:= Is_Str;
For i:=0 to 31-Length(Is_Str) do
Begin
Temp:='F'+Temp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -