Hi I am quite a new programmer and I was just wondering if there's anyone out there who could help me...

I need to write a procedure to input a decimal number via a suitable const parameter and output the hexadecimal equivalent to the console window...

Now, I can do the maths and figure I need to use a loop of some type, but can anyone help me on how i can store the cumbers separately to put together at the end of the procedure...?
you need to mod the number by 16 to get the remainder and then div by 16 and repeat with new num... how do you store the remainders and convert them into the hex equivalent without having lines and lines of code?

Dani AI

Generated

's suggestion to accumulate hex digits in a string is exactly the right idea, and 's example shows the straightforward case-based mapping. A slightly cleaner and more efficient approach is to use a small lookup string ('0123456789ABCDEF'), count how many hex digits you'll need, preallocate the result, and fill it from right to left. That avoids a long case block and repeated string-prefixing (which repeatedly copies the whole string).

function DecToHex(const Value: Int64): string;
const
  HexChars = '0123456789ABCDEF';
var
  tmp, temp: UInt64;
  digits, pos: Integer;
  negative: Boolean;
begin
  if Value = 0 then Exit('0');
  negative := Value < 0;
  if negative then tmp := UInt64(-Value) else tmp := UInt64(Value);
  temp := tmp;
  digits := 0;
  while temp > 0 do begin temp := temp div 16; Inc(digits); end;
  SetLength(Result, digits + Ord(negative));
  if negative then Result[1] := '-';
  pos := Length(Result);
  while tmp > 0 do
  begin
    Result[pos] := HexChars[(tmp mod 16) + 1];
    tmp := tmp div 16;
    Dec(pos);
  end;
end;

Notes and tips: handle the 0 case explicitly, decide how to treat negatives (above uses a leading -), and pick Int64/UInt64 if you expect large values. If your compiler lacks UInt64, use a signed type appropriate for your range (or use the append-then-reverse technique as a fallback). For console output simply call Writeln(DecToHex(yourNumber));.

Recommended Answers

All 2 Replies

I wrote one years ago where I stored the hex digits in a string.

program dec2hex;

{$APPTYPE CONSOLE}

uses
  SysUtils;

const
   BASE16 = 16;
var
   HexValue  : string;
   Remainder : Integer;
   Quotient  : Integer;
begin
   HexValue := '';
   write( 'Enter an integer value: ' );
   readln( Quotient );
   while Quotient > 0 do
   begin
      Remainder := Quotient mod BASE16;
      case Remainder of
         10: HexValue := 'A' + HexValue;
         11: HexValue := 'B' + HexValue;
         12: HexValue := 'C' + HexValue;
         13: HexValue := 'D' + HexValue;
         14: HexValue := 'E' + HexValue;
         15: HexValue := 'F' + HexValue;
      else
         HexValue := IntToStr( Remainder ) + HexValue;
      end;
      Quotient := Quotient div BASE16
   end;
   writeln;
   writeln( HexValue );
   writeln;
end.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.