SRL Archive - Research & Development Lounge

IntToHex & HexToInt


mastaraymond

2007-08-29 18:24:21

I made some hex functions and i thought that maybe someone needs them! The $ is only usefull for scar, for example: bla := 10;(Integer) bla := $A;(Hex).
Newer:
program Hex;
function HexToInt(hex : String): Integer;
var
Bla : String;
begin;
Bla := '$' + TrimOthers(Hex);
Result := StrToInt(Bla);
if pos('-',hex) >0 then result :=-result;
end;

function IntToHex(int : integer): string;
var
Hexx : string;
Bla : integer;
begin;
Hexx := '0123456789ABCDEF';
Bla := iAbs(int);
while not (bla div 16 = 0) do
begin;
result := Hexx[bla mod 16 + 1] + result;
bla := bla div 16;
end;
if int < 0 then result := '-$' + Hexx[bla+1] + result
else result := '$' + Hexx[bla+1] + result;
end;

begin
Writeln('The $ is for scar, not for the hex number itself! - MastaRaymond');
Writeln(IntToHex(16777215));
Writeln(IntToStr(HexToInt('$FFFFFF')));
end.
Old:
program New;

function ConvertHex(Hex : string) : integer;
begin;
case lowercase(Hex) of
'a': result := 10;
'b': result := 11;
'c': result := 12;
'd': result := 13;
'e': result := 14;
'f': result := 15;
end;
if StrToIntDef(Hex,11) < 10 then Result := StrToInt(Hex);
end;

function HexToInt(hex : String): Integer;
var
Bla : String;
I,J : Integer;
begin;
Bla := TrimOthers(Hex);
J := Length(Bla);
For I := 0 to Length(Bla)-1 do
Result := Result +(Round(Pow(16,I)) * ConvertHex(Bla[J-I]));
if pos('-',hex) >0 then result :=-result;
end;

function ConvertInt(Int : Integer): string;
begin;
case Int of
10: result := 'A';
11: result := 'B';
12: result := 'C';
13: result := 'D';
14: result := 'E';
15: result := 'F';
end;
if Int < 10 then Result := inttostr(Int);
end;

function IntToHex(int : integer): string;
var
Bla : integer;
begin;
Bla := iAbs(int);
while not (bla div 16 = 0) do
begin;
result := convertInt(bla mod 16) + result;
bla := bla div 16;
end;
result := convertInt(bla) + result;
if int < 0 then result := '-' + result;
result := '$' + result;
end;

begin
Writeln('The $ is for scar, not for the hex number itself! - MastaRaymond');
Writeln(IntToHex(16238230));
Writeln(IntToStr(HexToInt('$F7C696')));
end.

ShowerThoughts

2007-08-29 18:29:09

?what it does im not that good on english what is hex?

mastaraymond

2007-08-30 05:21:53

From: hermpie
?what it does im not that good on english what is hex?


Hex is a different way to write numbers, decimals (what we use) has got the digits [0,1,2,3,4,5,6,7,8,9], hex has got the digits [0,1,2,3,4,5,6,7,8,9,A(10),B(11),C(12),D(13),E(14),F(15)]. Because were used to Decimals, Hex is hard to read, without converting it. So if you want to protect your 'Numbers' in a script (*Cough* Wizzup's Draynor Fisher), you can use this function to find the Hex, but thats not the reason why i made this. You will find out.. soon?

~Raymond

n3ss3s

2007-08-30 08:01:07

Haha, in MSI I saw in wizzups fish finding function him using hex :p

Wizzup?

2007-08-30 08:36:26

From: mastaraymond
Hex is a different way to write numbers, decimals (what we use) has got the digits [0,1,2,3,4,5,6,7,8,9], hex has got the digits [0,1,2,3,4,5,6,7,8,9,A(10),B(11),C(12),D(13),E(14),F(15)]. Because were used to Decimals, Hex is hard to read, without converting it. So if you want to protect your 'Numbers' in a script (*Cough* Wizzup's Draynor Fisher), you can use this function to find the Hex, but thats not the reason why i made this. You will find out.. soon?

~Raymond


You can just inttostr a hex…

WriteLn(IntToStr($43));

Plus, windows calculator extended for Hex <> Bin <> Dec..

ShowerThoughts

2007-08-30 09:35:35

sort encrytion you can make a author on a script?

Freddy1990

2007-08-30 12:28:36

program Hex;

function IntToHex(Int: Integer): string;
var
i: Integer;
Hex: string;
begin;
Hex := '0123456789ABCDEF';
i := iAbs(Int);
while not (i div 16 = 0) do
begin;
Result := Hex[i mod 16 + 1] + Result;
i := i div 16;
end;
Result := Hex[i + 1] + Result;
end;

function HexToInt(Hex: string): Integer;
var
s: string;
begin;
s := Trim(Hex);
if not (Hex[1] = '$') then s := '$' + s;
Result := StrtoInt(s);
end;

begin
Writeln(IntToHex(16238230));
Writeln(IntToStr(HexToInt('F7C696')));
end.
Shorter…

mastaraymond

2007-08-30 12:41:39

Hehe. Thats shorter, but it doesnt work for minus numbers ;).

EDIT: /Me wins :
program Hex;
function HexToInt(hex : String): Integer;
var
Bla : String;
begin;
Bla := '$' + TrimOthers(Hex);
Result := StrToInt(Bla);
if pos('-',hex) >0 then result :=-result;
end;

function IntToHex(int : integer): string;
var
Hexx : string;
Bla : integer;
begin;
Hexx := '0123456789ABCDEF';
Bla := iAbs(int);
while not (bla div 16 = 0) do
begin;
result := Hexx[bla mod 16 + 1] + result;
bla := bla div 16;
end;
if int < 0 then result := '-$' + Hexx[bla+1] + result
else result := '$' + Hexx[bla+1] + result;
end;

begin
Writeln('The $ is for scar, not for the hex number itself! - MastaRaymond');
Writeln(IntToHex(16777215));
Writeln(IntToStr(HexToInt('$FFFFFF')));
end.

the scar noob

2007-08-30 14:57:38

ok Hex to normal, i know it was possible with a simple writeln, but it's cool i can use the other way now :)