A G E N T
2008-12-07 15:44:18
Hi,
Made some quick functions, I am certain that the Hex <–> Int ones work fine, not sure if the string ones are accurate. Unfortunately I am handling hex as strings.
Made some quick functions, I am certain that the Hex <–> Int ones work fine, not sure if the string ones are accurate. Unfortunately I am handling hex as strings.
program New;
{.include SRL/SRL.scar}
function Reverse(str:string):string;
var
i, l : integer;
begin
l := Length(str);
SetLength(Result, l);
for i := 1 to l do
begin
Result[i] := str[(l+1)-i];
end;
end;
function IntToHex(int:integer):string;
var
remainder, res : integer;
hex, greater : string;
begin
greater := 'ABCDEF';
res := int div 16;
remainder := int mod 16;
if(remainder > 9)then
hex := greater[remainder-9]
else
hex := inttostr(remainder);
if(res = 0)then
begin
Result := reverse(hex);
Exit;
end;
repeat
remainder := res mod 16;
res := res div 16;
if(remainder > 9)then
hex := hex + greater[remainder-9]
else
hex := hex + inttostr(remainder);
if(res = 0)then
begin
Result := reverse(hex);
Exit;
end;
until(res=0);
end;
function HexToInt(hex:string):extended;
var
i, l : integer;
begin
Result := 0;
l := Length(hex);
for i := 1 to l do
begin
Result := Result + (StrToFloat(hex[(l+1)-i]) * (Pow(16, i-1)));
end;
end;
function StrToHex(str:string):String;
var
i : integer;
begin
Result := '';
for i := 1 to length(str) do
begin
Result := Result + inttohex(ord(str[i]));
end;
end;
function HexToStr(hex:string):string;
var
i : integer;
begin
for i := 1 to length(hex) - 1 do
begin
Result := Result + chr(Round(HexToInt(hex[i]+hex[i+1])));
end;
end;
begin
//writeln(inttostr($111));
writeln(IntToHex(188));
writeln(StrToHex('BC'));
writeln(floattostr(hextoint('1128')));
writeln(HexToStr('1128'));
end.