# converting into hex

1. how can i convert a string into hex ?
2. how can i convert an integer into hex ?

thanks,
must!

• : 1. how can i convert a string into hex ?
: 2. how can i convert an integer into hex ?
:
: thanks,
: must!
:

Just search this board, there are tons of examples
showing how to convert from and to any format.

Check this out, it might work:

[code]
procedure int2hex(i:integer):string;
var n:integer;
s:string;
const lookup:string='0123456789ABCDEF';
begin
n:=i;
s:='';
while n>0 do
begin
s:=s+lookup[n mod 10];
n:=n div 10;
end;
end;

[/code]

Converting back works the same way, jus use POS() fo find out
position of hex character in lookup table (to be on safe side,
convert it to upercase first).

Iby

• The previous post was incorrect (it was supposed to be
function instead of procedure, there is no value assigned
to function output etc....).
Hmmm... Not bad after 3_day_party :-)
Ok so here is what you asked for, this time it supports
up to 32bit numbers:

[code]

function int2hex(i:longint):string;
var n:longint;
s:string;
const lookup:string='0123456789ABCDEF';

begin
n:=i;
if i=0 then s:='0000' else s:='';
while n>0 do
begin
s:=lookup[n mod 16+1]+s;
n:=n div 16;
end;
while length(s)<4 do s:='0'+s;
int2hex:=s;
end;

function hex2int(s:string):longint;
var i:longint;
j:integer;
const lookup:string='0123456789ABCDEF';

begin
i:=0;
for j:=1 to length(s) do
i:=i*16+pos(upcase(s[j]),lookup)-1;
hex2int:=i;
end;

[/code]

Iby

• I'm using this code :

Program Hex2St_prg;
Const Hex : String = "0123456789ABCDEF";

Function Hex2Str_1(Input : Byte):String;
Begin
Hex2Str_1 := Hex[Lo(Input)+1]+Hex[Hi(Input)+1];
End;

Function Hex2Str(Input : Integer):String;
Begin
Hex2Str_2 := Hex2Str_1(Lo(Input))+Hex2Str_1(Hi(Input));
End;

Begin
Hex2Str();
End;