program HexToDecimal;
var
hex: string;
decimal, i, value, base: integer;
function HexValue(ch: char): integer;
begin
if (ch >= '0') and (ch <= '9') then
HexValue := Ord(ch) - Ord('0')
else if (ch >= 'A') and (ch <= 'F') then
HexValue := Ord(ch) - Ord('A') + 10
else if (ch >= 'a') and (ch <= 'f') then
HexValue := Ord(ch) - Ord('a') + 10
else
HexValue := -1;
end;
begin
hex := '1D7F';
decimal := 0;
base := 1;
for i := Length(hex) downto 1 do
begin
value := HexValue(hex[i]);
if value = -1 then
begin
writeln('Invalid hexadecimal number.');
exit;
end;
decimal := decimal + value * base;
base := base * 16;
end;
writeln('Decimal value: ', decimal);
end.
(*
run:
Decimal value: 7551
*)