home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 November
/
Chip_2002-11_cd1.bin
/
zkuste
/
delphi
/
unity
/
d56
/
DW
/
DW10242.ZIP
/
NumWorks.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
2002-08-08
|
6KB
|
242 lines
(*-------------------------------NumWorks.pas--------------------------
V1.0.240 - 09.08.2002 - current release
*)
unit NumWorks;
interface
uses Windows, SysUtils, StringWorks;
(*1.0.239*)
function DecToRoman(iDecimal: LongInt): String;
function Expon(const Value, Exponent: Integer): Integer;
function FreeNotationToFreeNotation(const Value, SrcNotationConfig, DstNotationConfig: String): String;
function FreeNotationToInt(const Value, NotationConfig: String): Integer;
function IntToFreeNotation(const Value: Integer; const NotationConfig: String): String;
function ValidateValueForFreeNotation(const Value, NotationConfig: String): Boolean;
(*1.0.238*)
function SimpleChecksum(const Str: String): Integer;
(*1.0.237*)
function Diff(const Value1, Value2: Integer): Integer;
function RoundUp(X: Real): Integer;
function RoundDown(X: Real): Integer;
function RndBetween(const RangeMinor, RangeMajor: Integer): Integer;
function HexToInt(HexStr: String): Integer;
function ExtractBits(const Value, Start, Count: Integer): Integer;
function CountBits(const Value: Integer): Integer;
function BitIsSet(w : DWord; Bitnr:integer):Boolean;
procedure SetBit(var w : DWord; Bitnr:integer);
procedure ResetBit(var w : DWord; Bitnr:integer);
const
DW_NOTATION_BIN: String = '01';
DW_NOTATION_DEC: String = '0123456789';
DW_NOTATION_HEX: String = '0123456789ABCDEF';
implementation
function DecToRoman(iDecimal: LongInt): String;
const
aRomans: array [ 1..13 ] of String = ( 'I', 'IV', 'V',
'IX', 'X', 'XL','L', 'XC', 'C', 'CD', 'D', 'CM', 'M' );
aArabics: array [ 1..13 ] of Integer = ( 1, 4, 5,
9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 );
var
I: Integer;
begin
for I := 13 downto 1 do begin
while (iDecimal >= aArabics[I]) do begin
iDecimal := iDecimal - aArabics[I];
result := result + aRomans[I];
end;
end;
end;
function Expon(const Value, Exponent: Integer): Integer;
var i: Integer;
wert: Integer;
begin
wert:=Value;
if Exponent=0 then
begin
result:=1;
exit;
end
else
begin
for i:=1 to Exponent-1 do
begin
wert:=wert*Value;
end; {for}
result:=wert;
end;
end;
function FreeNotationToFreeNotation(const Value, SrcNotationConfig, DstNotationConfig: String): String;
begin
result:= IntToFreeNotation(FreeNotationToInt(
Value,
SrcNotationConfig),
DstNotationConfig);
end;
function FreeNotationToInt(const Value, NotationConfig: String): Integer;
var
iBase, iPot, iVal, iValue, I: Integer;
sValue: String;
begin
iValue:= 0;
sValue:= ReverseStr(Value);
iBase:= Length(NotationConfig);
for I:= 0 to Length(sValue) - 1 do begin
iVal:= Pos(sValue[I+1], NotationConfig) - 1;
iPot:= Expon(iBase, I);
iValue:= iValue + (iVal*(iPot));
end;
result:= iValue;
end;
function IntToFreeNotation(const Value: Integer; const NotationConfig: String): String;
var
iBase, iDiv, iMod, iValue: Integer;
begin
iDiv:= -1;
iValue:= Value;
iBase:= Length(NotationConfig);
while iDiv <> 0 do begin
iMod:= iValue mod iBase;
iDiv:= iValue div iBase;
result:= result + NotationConfig[iMod+1];
iValue:= iDiv;
end;
result:= ReverseStr(result);
end;
function ValidateValueForFreeNotation(const Value, NotationConfig: String): Boolean;
var
I: Integer;
begin
result:= FALSE;
for I:= 1 to Length(Value) do begin
result:= (Pos(Value[I], NotationConfig) <> 0);
if not result then Break;
end;
end;
function SimpleChecksum(const Str: String): Integer;
var
I, Value: Integer;
Chk: Boolean;
begin
Chk:= FALSE;
result:= 0;
Value:= 0;
if Length(Str) < 1 then exit;
for I:= 1 to Length(Str) do begin
if Chk then Value:= Value + (Ord(Str[I]) * 7) else
Value:= Value + (Ord(Str[I]) * 3);
Chk:= not Chk;
end;
result:= Value;
end;
function Diff(const Value1, Value2: Integer): Integer;
begin
if (Value1 > Value2) then
result:= Value1 - Value2
else
result:= Value2 - Value1;
end;
function RoundUp(X: Real): Integer;
begin
if Trunc(X)<>X then begin
result:=Trunc(X)+1;
exit;
end else result:= Trunc(X);
end;
function RoundDown(X: Real): Integer;
begin
if Trunc(X)<>X then begin
result:=Trunc(X)-1;
exit;
end else result:= Trunc(X);
end;
function RndBetween(const RangeMinor, RangeMajor: Integer): Integer;
var
Rnd: Integer;
begin
Rnd:= RangeMinor + Random(RangeMajor - RangeMinor);
result:= Rnd;
end;
function HexToInt(HexStr: String): Integer;
begin
result:= StrToInt('$' + HexStr);
end;
function ExtractBits(const Value, Start, Count: Integer): Integer;
const
{basiert auf Guido Gybels, http://www.optimalcode.com/Guido/basmex6.html}
Mask: array[0..31] of Int64 =
($01,$03,$07,$0F,$1F,$3F,$7F,$FF,
$01FF,$03FF,$07FF,$0FFF,$1FFF,$3FFF,$7FFF,$FFFF,
$01FFFF,$03FFFF,$07FFFF,$0FFFFF,
$1FFFFF,$3FFFFF,$7FFFFF,$FFFFFF,
$01FFFFFF,$03FFFFFF,$07FFFFFF,$0FFFFFFF,
$1FFFFFFF,$3FFFFFFF,$7FFFFFFF,$FFFFFFFF);
asm
xchg ecx,edx
test edx,edx
jnz @@isoke
xor eax,eax
jmp @@ending
@@isoke:
dec edx
and edx,31
shr eax,cl
and eax,dword ptr [Mask+edx*4]
@@ending:
end;
function CountBits(const Value: Integer): Integer;
asm
{basiert auf Guido Gybels, http://www.optimalcode.com/Guido/basmex6.html}
mov ecx,eax
xor eax,eax
test ecx,ecx
jz @@ending
@@counting:
shr ecx,1
adc eax,0
test ecx,ecx
jnz @@counting
@@ending:
end;
function BitIsSet(w : DWord; Bitnr:integer):Boolean;
begin
{basiert auf WernerSt, Spotlight Delphi Forum, 17.12.2001}
result:=(w and (1 shl Bitnr))<>0;
end;
procedure SetBit(var w : DWord; Bitnr:integer);
begin
{basiert auf WernerSt, Spotlight Delphi Forum, 17.12.2001}
w:=w or (1 shl Bitnr);
end;
procedure ResetBit(var w : DWord; Bitnr:integer);
begin
{basiert auf WernerSt, Spotlight Delphi Forum, 17.12.2001}
w:=w and ($FFFFFFFF xor (1 shl Bitnr));
end;
end.