Converting Dec to Hex without using IntToHex...

Reply

Join Date: Dec 2004
Posts: 1
Reputation: dirtyk is an unknown quantity at this point 
Solved Threads: 0
dirtyk dirtyk is offline Offline
Newbie Poster

Converting Dec to Hex without using IntToHex...

 
0
  #1
Dec 1st, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 207
Reputation: Real-tiner is an unknown quantity at this point 
Solved Threads: 8
Real-tiner Real-tiner is offline Offline
Posting Whiz in Training

Re: Converting Dec to Hex without using IntToHex...

 
0
  #2
Jan 18th, 2005
I wrote one years ago where I stored the hex digits in a string.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: Jackrabbit is an unknown quantity at this point 
Solved Threads: 0
Jackrabbit Jackrabbit is offline Offline
Light Poster

Re: Converting Dec to Hex without using IntToHex...

 
0
  #3
Feb 2nd, 2005
Pascal and Delphi Syntax (Toggle Plain Text)
  1. program dec2hex;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils;
  7.  
  8. const
  9. BASE16 = 16;
  10. var
  11. HexValue : string;
  12. Remainder : Integer;
  13. Quotient : Integer;
  14. begin
  15. HexValue := '';
  16. write( 'Enter an integer value: ' );
  17. readln( Quotient );
  18. while Quotient > 0 do
  19. begin
  20. Remainder := Quotient mod BASE16;
  21. case Remainder of
  22. 10: HexValue := 'A' + HexValue;
  23. 11: HexValue := 'B' + HexValue;
  24. 12: HexValue := 'C' + HexValue;
  25. 13: HexValue := 'D' + HexValue;
  26. 14: HexValue := 'E' + HexValue;
  27. 15: HexValue := 'F' + HexValue;
  28. else
  29. HexValue := IntToStr( Remainder ) + HexValue;
  30. end;
  31. Quotient := Quotient div BASE16
  32. end;
  33. writeln;
  34. writeln( HexValue );
  35. writeln;
  36. end.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC