1,105,386 Community Members

Converting Dec to Hex without using IntToHex...

Member Avatar
dirtyk
Newbie Poster
1 post since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
Real-tiner
Posting Whiz in Training
207 posts since Dec 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Jackrabbit
Light Poster
31 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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.
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article