Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
writing a procedure for dec to hex Posted by DirtyK on 1 Dec 2004 at 11:54 AM
Yes I'm here again...

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 numbers separately within the loop to put together at the end of the procedure...?
For example... 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?

Any advice would be appreciated... As I've said before I am pretty new to this and really wanna learn the language...
Report
Re: writing a procedure for dec to hex Posted by zibadian on 1 Dec 2004 at 12:24 PM
: Yes I'm here again...
:
: 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 numbers separately within the loop to put together at the end of the procedure...?
: For example... 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?
:
: Any advice would be appreciated... As I've said before I am pretty new to this and really wanna learn the language...
:

here is a sample code:
var
  i: integer;
  Number: integer;
  HexValue: string;
begin
  HexStr := '';
  while Number > 0 do
  begin
    HexStr := HexValues[Number mod 16+1] + HexStr;
    Number := Number div 16+1
  end;
end;

I'm not sure if the resulting hex string will be inverted or not. The code should be easy enough to follow and understand.
Report
Re: writing a procedure for dec to hex Posted by DirtyK on 1 Dec 2004 at 2:28 PM
: : Yes I'm here again...
: :
: : 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 numbers separately within the loop to put together at the end of the procedure...?
: : For example... 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?
: :
: : Any advice would be appreciated... As I've said before I am pretty new to this and really wanna learn the language...
: :
:
: here is a sample code:
:
: var
:   i: integer;
:   Number: integer;
:   HexValue: string;
: begin
:   HexStr := '';
:   while Number > 0 do
:   begin
:     HexStr := HexValues[Number mod 16+1] + HexStr;
:     Number := Number div 16+1
:   end;
: end;
: 

: I'm not sure if the resulting hex string will be inverted or not. The code should be easy enough to follow and understand.
:
should i declare the hexstr(ie the values) as a const within the procedure?

And again how do i call to the procedure from a user input?


Report
Re: writing a procedure for dec to hex Posted by DirtyK on 1 Dec 2004 at 2:43 PM
This message was edited by DirtyK at 2004-12-1 14:43:54

: : Yes I'm here again...
: :
: : 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 numbers separately within the loop to put together at the end of the procedure...?
: : For example... 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?
: :
: : Any advice would be appreciated... As I've said before I am pretty new to this and really wanna learn the language...
: :
:
: here is a sample code:
:
: var
:   i: integer;
:   Number: integer;
:   HexValue: string;
: begin
:   HexStr := '';
:   while Number > 0 do
:   begin
:     HexStr := HexValues[Number mod 16+1] + HexStr;
:     Number := Number div 16+1
:   end;
: end;
: 

: I'm not sure if the resulting hex string will be inverted or not. The code should be easy enough to follow and understand.
:

I have this code now does this seem fairly right?
Whereabouts and how do I read in the hex values...? And if this code isn't looking right... can you give me a pointer as to where am i going wrong?

uses
SysUtils;
var
user : integer;


procedure DecToHex (Number : integer);



var

Hexstr, prop : string;
HexValue: string;
begin
HexStr := '';
while Number > 0 do
begin
HexStr := HexValue[Number mod 16+1] + HexStr;
Number := Number div 16+1
end;
end;
begin
writeln (' enter a number');
readln (user);
DecToHex (user);



end.


cheers!






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.