writing a procedure for dec to hex - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

writing a procedure for dec to hex

DirtyKDirtyK Posts: 35Member
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...

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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:
    [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;
    [/code]
    I'm not sure if the resulting hex string will be inverted or not. The code should be easy enough to follow and understand.
  • DirtyKDirtyK Posts: 35Member
    : : 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:
    : [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;
    : [/code]
    : 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?


  • DirtyKDirtyK Posts: 35Member
    [b][red]This message was edited by DirtyK at 2004-12-1 14:43:54[/red][/b][hr]
    : : 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:
    : [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;
    : [/code]
    : 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!



Sign In or Register to comment.