Basic functions that Borland didn't implement.
I thought that it could come into hand for some of you.
The function names says it all.
There is no function in here for converting a integer into a hex string, since it's allready implemented by Borland. Just use IntToHex(10,2) = '0A' for such purpose.
function HexToInt(Hex : string) : Cardinal;
const cHex = '0123456789ABCDEF';
var mult,i,loop : integer;
begin
result := 0;
mult := 1;
for loop := length(Hex)do
wnto 1do
begin
i := pos(Hex[loop],cHex)-1;
if (i < 0) then
i := 0;
inc(result,(i*mult));
mult := mult * 16;
end;
end;
function IntToBin(Value : integer;
NumBits : byte) : string;
var lp0 : integer;
begin
result := '';
for lp0 := 0 to NumBits-1do
begin
if ((Value and (1 shl (lp0))) <> 0) then
begin
result := #$31 + result;
end else
result := #$30 + result;
end;
end;
function BinToInt(Bin : string) : integer;
var mult,lp0 : integer;
begin
result := 0;
mult := 0;
for lp0 := length(Bin)do
wnto 1do
begin
if Bin[lp0] = #$31 then
begin
inc(result,(1 shl mult));
end;
inc(mult);
end;
end;
function BinToHex(Bin : string;
Digits : integer) : string;
begin
result := IntToHex(BinToInt(Bin),Digits);
end;
function HexToBin(Hex : string;
NumBits : byte) : string;
begin
result := IntToBin(HexToInt(Hex),NumBits);
end;
// Below are also some code usefull for testing if a string is in binary, hex or decimal form.
function IsStrDec(Value : string) : boolean;
var lp0 : integer;
begin
// returns true on empty strings, since equal to zero
result := true;
for lp0 := 1 to length(Value)do
begin
result := result and ((Ord(Value[lp0]) >= $30) and (Ord(Value[lp0]) <= $39));
end;
end;
function IsStrHex(Value : string) : boolean;
var lp0 : integer;
begin
result := true;
for lp0 := 1 to length(Value)do
begin
case Ord(Upcase(Value[lp0])) of
$30..$39, $41..$46: result := result and true;
else
result := false;
end;
end;
end;
function IsStrBin(Value : string) : boolean;
var lp0 : integer;
begin
result := true;
for lp0 := 1 to length(Value)do
begin
case Ord(Upcase(Value[lp0])) of
$30..$31: result := result and true;
else
result := false;
end;
end;
end;