//
Unit ads_Conv; {Copyright(c)2016 Advanced Delphi Systems Richard Maley Advanced Delphi Systems 12613 Maidens Bower Drive Potomac, MD 20854 USA phone 301-840-1554 dickmaley@advdelphisys.com The code herein can be used or modified by anyone. Please retain references to Richard Maley at Advanced Delphi Systems. If you make improvements to the code please send your improvements to dickmaley@advdelphisys.com so that the entire Delphi community can benefit. All comments are welcome. } (*UnitIndex Master Index Implementation Section Download Units
Description: ads_Conv.pas This unit contains the following routines.
BCDAdd BCDDivide BCDMultiply BCDRemainder BCDSubtract BCDToInt ConvertBooleanToStringTF ConvertBooleanToStringTrueFalse ConvertBooleanToStringYesNo ConvertBooleanToStringYN ConvertIntegerToBinaryString ConvertPCharToString ConvertStringToBoolean ConvertStringToChar ConvertStringToInteger ConvertStringToPChar ConvertWordToBinaryString HexToCard HexToInt HexToInt64 IntToBCD TEditKeyFilter.OnlyAToZ TEditKeyFilter.OnlyNumbers TEditKeyFilter.OnlyNumbersAbsolute TPanel_Cmp_Sec_ads.ResizeShadowLabel
*) Interface Uses ExtCtrls, StdCtrls, SysUtils, Windows; Function BCDAdd(BCD1,BCD2: String): String; Function BCDDivide(dividend,divisor: String): String; Function BCDMultiply(BCD1,BCD2: String): String; Function BCDRemainder(dividend,divisor: String): String; Function BCDSubtract(BCDFrom,BCDMinus: String): String; Function BCDToInt(sgBCD: String): Integer; Function HexToInt(HexStr: String): Integer; Function HexToCard(HexStr: String): Cardinal; function HexToInt64(Hex: string): int64; Function IntToBCD(inValue: Integer): String; {!~ Converts a Boolean value to String where True = Y and False = N} Function ConvertBooleanToStringYN(bo : Boolean) : String; {!~ Converts a Boolean value to String where True = Yes and False = No} Function ConvertBooleanToStringYesNo(bo : Boolean) : String; {!~ Converts a Boolean value to String where True = T and False = F} Function ConvertBooleanToStringTF(bo : Boolean) : String; {!~ Converts a Boolean value to String where True = True and False = False} Function ConvertBooleanToStringTrueFalse(bo : Boolean) : String; {!~ Converts a String value to a Boolean} Function ConvertStringToBoolean(sg : String) : Boolean; {!~ Converts an integer value to its binary equivalent as a ShortString } Function ConvertIntegerToBinaryString(Int, Length : Integer) : ShortString; {!~ Converts A PChar To String} Function ConvertPCharToString(PCharValue: PChar): String; {!~ Converts A String To Char} Function ConvertStringToChar(InputString: String; CharPosition: Integer): Char; {!~ Converts A String To Integer, If An Error Occurrs The Function Returns -0} Function ConvertStringToInteger(StringValue: String): Integer; {!~ Converts A String To A PChar, If An Error Occurrs The Function Returns 0} Function ConvertStringToPChar(StringValue: String): PChar; {!~ Converts a word value to its binary equivalent as a ShortString } Function ConvertWordToBinaryString(InputWord : Word; Length : Integer) : ShortString; implementation Uses ads_Strg; Type TPanel_Cmp_Sec_ads = class(TPanel) Public procedure ResizeShadowLabel(Sender: TObject); End; //Unit Description UnitIndex Master Index
procedure TPanel_Cmp_Sec_ads.ResizeShadowLabel( Sender : TObject); Var PH, PW : Integer; LH, LW : Integer; begin PH := TPanel(Sender).Height; PW := TPanel(Sender).Width; LH := TLabel(Controls[0]).Height; LW := TLabel(Controls[0]).Width; TLabel(Controls[0]).Top := ((PH-LH) div 2)-3; TLabel(Controls[0]).Left := ((Pw-Lw) div 2)-3; end; Type TEditKeyFilter = Class(TEdit) Published {!~ Throws away all keys except 0-9,-,+,.} Procedure OnlyNumbers(Sender: TObject; var Key: Char); {!~ Throws away all keys except 0-9} Procedure OnlyNumbersAbsolute(Sender: TObject; var Key: Char); {!~ Throws away all keys except a-z and A-Z} Procedure OnlyAToZ(Sender: TObject; var Key: Char); End; {!~ Throws away all keys except 0-9,-,+,.} //Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyNumbers(Sender: TObject; var Key: Char); Begin KeyPressOnlyNumbers(Key); End; {!~ Throws away all keys except 0-9} //Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyNumbersAbsolute(Sender: TObject; var Key: Char); Begin KeyPressOnlyNumbersAbsolute(Key); End; {!~ Throws away all keys except a-z and A-Z} //Unit Description UnitIndex Master Index
Procedure TEditKeyFilter.OnlyAToZ(Sender: TObject; var Key: Char); Begin KeyPressOnlyAToZ(Key); End; {!~ Converts an integer value to its binary equivalent as a ShortString } //Unit Description UnitIndex Master Index
Function ConvertIntegerToBinaryString(Int, Length : Integer) : ShortString; Begin Result := ConvertWordToBinaryString(Word(Int),Length); End; {!~ The following example returns the binary value of 12 as a shortstring of length 8. ConvertIntegerToBinaryString(12, 8); } {!~ Converts A PChar To String} //Unit Description UnitIndex Master Index
Function ConvertPCharToString(PCharValue: PChar): String; Begin Result := StrPas(PCharValue); End; {!~ Converts A String To Char} //Unit Description UnitIndex Master Index
Function ConvertStringToChar(InputString: String; CharPosition: Integer): Char; Begin Result := InputString[CharPosition]; End; {!~ Converts A String To Integer, If An Error Occurrs The Function Returns -0} //Unit Description UnitIndex Master Index
Function ConvertStringToInteger(StringValue: String): Integer; Var I, Code: Integer; Begin VAL(StringValue, I, Code); {Was There An Error} If Not (Code=0) Then Begin {An Error Occurred} Result := 0; End Else Begin {Conversion Ran Properly} Result := I; End; End; {!~ Converts A String To A PChar, If An Error Occurrs The Function Returns 0} //Unit Description UnitIndex Master Index
Function ConvertStringToPChar(StringValue: String): PChar; Var PCharString: Array[0..255] of Char; Begin Result := StrPCopy(PCharString,StringValue); End; {!~ Converts a word value to its binary equivalent as a ShortString } //Unit Description UnitIndex Master Index
Function ConvertWordToBinaryString(InputWord : Word; Length : Integer) : ShortString; var Counter, Number : Cardinal; D : Array[0..1] of AnsiChar; Begin D[0] := '0'; D[1] := '1'; Number := 1; Result[0] := #16; For Counter := 15 Downto 0 Do Begin Result[Number] := D[Ord(InputWord and (1 shl Counter) <> 0)]; Inc(Number); End; If Length > 16 Then Length := 16; If Length < 1 Then Length := 1; Result := SubStr(Result,16-Length,Length); End; {!~ Converts a Boolean value to String where True = Y and False = N} //Unit Description UnitIndex Master Index
Function ConvertBooleanToStringYN(bo : Boolean) : String; Begin If bo Then Result := 'Y' Else Result := 'N'; End; {!~ Converts a Boolean value to String where True = Yes and False = No} //Unit Description UnitIndex Master Index
Function ConvertBooleanToStringYesNo(bo : Boolean) : String; Begin If bo Then Result := 'Yes' Else Result := 'No'; End; {!~ Converts a Boolean value to String where True = T and False = F} //Unit Description UnitIndex Master Index
Function ConvertBooleanToStringTF(bo : Boolean) : String; Begin If bo Then Result := 'T' Else Result := 'F'; End; {!~ Converts a Boolean value to String where True = True and False = False} //Unit Description UnitIndex Master Index
Function ConvertBooleanToStringTrueFalse(bo : Boolean) : String; Begin If bo Then Result := 'True' Else Result := 'False'; End; {!~ Converts a String value to a Boolean} //Unit Description UnitIndex Master Index
Function ConvertStringToBoolean(sg : String) : Boolean; Begin Result := False; sg := UpperCase(sg); If (sg = 'T') Or (sg = 'Y') Or (sg = 'TRUE') Or (sg = 'YES') Then Result := True; End; //Unit Description UnitIndex Master Index
Function IntToBCD(inValue: Integer): String; Var sg : String; inCounter : Integer; inLen : Integer; BCD : Array of String; sgChar : String; Begin Result := ''; SetLength(BCD,10); BCD[0] := '0000'; BCD[1] := '0001'; BCD[2] := '0010'; BCD[3] := '0011'; BCD[4] := '0100'; BCD[5] := '0101'; BCD[6] := '0110'; BCD[7] := '0111'; BCD[8] := '1000'; BCD[9] := '0001'; sg := IntToStr(inValue); inLen := Length(sg); For inCounter := 1 To inLen Do Begin sgChar := Copy(sg,inCounter,1); If sgChar='0' Then Result := Result + BCD[0]; If sgChar='1' Then Result := Result + BCD[1]; If sgChar='2' Then Result := Result + BCD[2]; If sgChar='3' Then Result := Result + BCD[3]; If sgChar='4' Then Result := Result + BCD[4]; If sgChar='5' Then Result := Result + BCD[5]; If sgChar='6' Then Result := Result + BCD[6]; If sgChar='7' Then Result := Result + BCD[7]; If sgChar='8' Then Result := Result + BCD[8]; If sgChar='9' Then Result := Result + BCD[9]; End; End; //Unit Description UnitIndex Master Index
Function BCDToInt(sgBCD: String): Integer; Var sg : String; inCounter : Integer; inLen : Integer; BCD : Array of String; sgChar : String; inMod : Integer; sgResult : String; inMax : Integer; Begin Result := 0; sgResult := ''; sg := sgBCD; SetLength(BCD,10); BCD[0] := '0000'; BCD[1] := '0001'; BCD[2] := '0010'; BCD[3] := '0011'; BCD[4] := '0100'; BCD[5] := '0101'; BCD[6] := '0110'; BCD[7] := '0111'; BCD[8] := '1000'; BCD[9] := '0001'; inLen := Length(sgBCD); inMod := inLen mod 4; inMax := inLen div 4; If inMod <> 0 Then Exit; For inCounter := 1 To inMax Do Begin sgChar := Copy(sg,1,4); If inCounter <> inMax Then sg := Copy(sg,5,Length(sg)-4); If sgChar=BCD[0] Then sgResult := sgResult + '0'; If sgChar=BCD[1] Then sgResult := sgResult + '1'; If sgChar=BCD[2] Then sgResult := sgResult + '2'; If sgChar=BCD[3] Then sgResult := sgResult + '3'; If sgChar=BCD[4] Then sgResult := sgResult + '4'; If sgChar=BCD[5] Then sgResult := sgResult + '5'; If sgChar=BCD[6] Then sgResult := sgResult + '6'; If sgChar=BCD[7] Then sgResult := sgResult + '7'; If sgChar=BCD[8] Then sgResult := sgResult + '8'; If sgChar=BCD[9] Then sgResult := sgResult + '9'; End; Result := StrToInt(sgResult); End; //Unit Description UnitIndex Master Index
Function BCDAdd(BCD1,BCD2: String): String; Begin Result := IntToBCD(BCDToInt(BCD1)+BCDToInt(BCD2)); End; //Unit Description UnitIndex Master Index
Function BCDSubtract(BCDFrom,BCDMinus: String): String; Begin Result := IntToBCD(BCDToInt(BCDFrom)-BCDToInt(BCDMinus)); End; //Unit Description UnitIndex Master Index
Function BCDDivide(dividend,divisor: String): String; Var inDivident: Integer; inDivisor : Integer; Begin Result := '0000'; inDivident:= BCDToInt(dividend); inDivisor := BCDToInt(divisor); If inDivident = 0 Then Exit; If inDivisor = 0 Then Exit; Result := IntToBCD(BCDToInt(dividend) div BCDToInt(divisor)); End; //Unit Description UnitIndex Master Index
Function BCDMultiply(BCD1,BCD2: String): String; Begin Result := IntToBCD(BCDToInt(BCD1)*BCDToInt(BCD2)); End; //Unit Description UnitIndex Master Index
Function BCDRemainder(dividend,divisor: String): String; Var inDivident: Integer; inDivisor : Integer; Begin Result := '0000'; inDivident:= BCDToInt(dividend); inDivisor := BCDToInt(divisor); If inDivident = 0 Then Exit; If inDivisor = 0 Then Exit; Result := IntToBCD(BCDToInt(dividend) mod BCDToInt(divisor)); End; //Unit Description UnitIndex Master Index
Function HexToInt(HexStr: String): Integer; Var sgFirst : String; Begin sgFirst := Copy(HexStr,1,1); If sgFirst <> '$' Then HexStr := '$' + HexStr; Result := StrToInt(HexStr); End; //Unit Description UnitIndex Master Index
Function HexToCard(HexStr: String): Cardinal; Begin Result := HexToInt64(HexStr); End; //Unit Description UnitIndex Master Index
function HexToInt64(Hex: string): int64; var i: integer; HexValues: String; begin HexValues:= '0123456789ABCDEF'; Result := 0; case Length(Hex) of 0: Result := 0; 1..16: for i:=1 to Length(Hex) do Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1; else for i:=1 to 16 do Result := 16*Result + Pos(Upcase(Hex[i]), HexValues)-1; end; end; End. //