Decimal to Hex and back again
const
searchstring = '0123456789ABCDEF';
procedure decimaltohex;
var
decimal : integer;
remainder : integer;
value : string;
begin
value := '';
write('Enter a decimal number:');
readln(decimal);
while decimal <> 0 do
begin
remainder := decimal and $F;
decimal := decimal shr 4;
// remainder := decimal mod 16;
// decimal := decimal div 16;
value := searchstring[remainder+1]+value;
end;
writeln('Hex value is:',value);
readln;
end;
procedure hextodecimal;
var
hex : string;
value : integer;
i,position:integer;
begin
value := 0;
write('Enter a Hex number:');
readln(hex);
for i := 1 to length(hex) do
begin
position := pos(hex[i],searchstring)-1;
value := (value * 16) + position;
end;
writeln('Decimal value is:',value);
readln;
end;
//-----------------------------------------------------------------------------------------
function dectobinary(decimalno:integer) : string;
var
binarystring : string;
begin
binarystring := '';
while decimalno > 0 do
begin
if decimalno and $1 = 0 then
binarystring := '0' + binarystring
else
binarystring := '1' +binarystring;
decimalno := decimalno shr 1;
end;
dectobinary := binarystring;
end;
function binarytodec(binarystring:string) : integer;
var
decimalno : integer;
i : integer;
m : integer;
begin
decimalno := 0;
m:= 1;
for i := length(binarystring) downto 1 do
begin
if binarystring[i] = '1' then
decimalno := decimalno + m;
m := m shl 1;
end;
binarytodec := decimalno;
end;
begin
writeln(binarytodec(dectobinary(49))) ;
readln;
end.