Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,003 questions

51,950 answers

573 users

How to convert hexadecimal to decimal in Pascal

2 Answers

0 votes
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

*)

 



answered Feb 14, 2025 by avibootz
0 votes
program HexToDecimal;

uses
  SysUtils;

var
  hex: string;
  dec: LongInt;

begin
  hex := '0x1D7F';
  
  // Remove the '0x' prefix
  hex := Copy(hex, 3, Length(hex) - 2);
  
  // Convert the hexadecimal string to a decimal integer
  dec := StrToInt('$' + hex);

  WriteLn(dec);
end.




(*
run:

7551

*)

 



answered Feb 14, 2025 by avibootz
...