convert decimal to hexadecimal number

Discussion in 'C Programming' started by sweeet_addiction16@yahoo.com, Sep 2, 2007.

  1. Guest

    hello
    Im writin a code in c...
    can sum1 pls help me out in writing a c code to convert decimalnumber
    to hexadecimal number.The hexadecimal number generated has to be an
    unsigned long.
     
    , Sep 2, 2007
    #1
    1. Advertising

  2. said:

    > hello
    > Im writin a code in c...
    > can sum1 pls help me out in writing a c code to convert decimalnumber
    > to hexadecimal number.The hexadecimal number generated has to be an
    > unsigned long.


    Numbers don't have bases. Number /representations/ have bases.

    It sounds to me like you wish to accept a textual representation in base
    ten of an integer, and present that integer's value using a base
    sixteen representation. Look up fgets (to capture the data as a
    string), strtoul (to convert it to an unsigned long integer), and
    printf (to display it in a base sixteen representation). You will find
    the %lX format specifier helpful.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 2, 2007
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    > hello
    > Im writin a code in c...
    > can sum1 pls help me out in writing a c code to convert decimalnumber
    > to hexadecimal number.The hexadecimal number generated has to be an
    > unsigned long.


    If your number is already in a numeric variable, just use sprintf or printf
    or fprintf to convert to hexadecimal representation. The relevant format
    specifier is lx. For example.

    unsigned int num = 10;
    printf("%lx\n", num);

    If your number is still in a textual form, then you can use sscanf or
    strtoul to convert it into a numeric value and then use the method above to
    print it out in hexadecimal.
     
    santosh, Sep 2, 2007
    #3
  4. Guest

    On Sep 2, 5:12 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > hello
    > > Im writin a code in c...
    > > can sum1 pls help me out in writing a c code to convert decimalnumber
    > > to hexadecimal number.The hexadecimal number generated has to be an
    > > unsigned long.

    >
    > Numbers don't have bases. Number /representations/ have bases.
    >
    > It sounds to me like you wish to accept a textual representation in base
    > ten of an integer, and present that integer's value using a base
    > sixteen representation. Look up fgets (to capture the data as a
    > string), strtoul (to convert it to an unsigned long integer), and
    > printf (to display it in a base sixteen representation). You will find
    > the %lX format specifier helpful.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999




    Im sry may be i didnot frame the question properly..i need to accept
    an integer value(decimal) and then after converting it into
    hexadecimal value i need to write it into a file.i do not need to
    print it..so using fprintf along with %lx would not help me.for eg..if
    i have a decimal value of 60 to be passed to a function ..i need that
    function to convert it into hexadecimal value(eg 3c) and then write it
    into a file
     
    , Sep 2, 2007
    #4
  5. Guest

    On Sep 2, 6:32 pm, santosh <> wrote:
    > wrote:
    > > hello
    > > Im writin a code in c...
    > > can sum1 pls help me out in writing a c code to convert decimalnumber
    > > to hexadecimal number.The hexadecimal number generated has to be an
    > > unsigned long.

    >
    > If your number is already in a numeric variable, just use sprintf or printf
    > or fprintf to convert to hexadecimal representation. The relevant format
    > specifier is lx. For example.
    >
    > unsigned int num = 10;
    > printf("%lx\n", num);
    >
    > If your number is still in a textual form, then you can use sscanf or
    > strtoul to convert it into a numeric value and then use the method above to
    > print it out in hexadecimal.


    Im sry may be i didnot frame the question properly..i need to accept
    an integer value(decimal) and then after converting it into
    hexadecimal value i need to write it into a file.i do not need to
    print it..so using fprintf along with %lx would not help me.for eg..if
    i have a decimal value of 60 to be passed to a function ..i need that
    function to convert it into hexadecimal value(eg 3c) and then write it
    into a file
     
    , Sep 2, 2007
    #5
  6. Thad Smith Guest

    wrote:
    > On Sep 2, 5:12 pm, Richard Heathfield <> wrote:
    >> said:
    >>
    >>> hello
    >>> Im writin a code in c...
    >>> can sum1 pls help me out in writing a c code to convert decimalnumber
    >>> to hexadecimal number.The hexadecimal number generated has to be an
    >>> unsigned long.

    >> Numbers don't have bases. Number /representations/ have bases.
    >>
    >> It sounds to me like you wish to accept a textual representation in base
    >> ten of an integer, and present that integer's value using a base
    >> sixteen representation. Look up fgets (to capture the data as a
    >> string), strtoul (to convert it to an unsigned long integer), and
    >> printf (to display it in a base sixteen representation). You will find
    >> the %lX format specifier helpful.

    >
    > Im sry may be i didnot frame the question properly..i need to accept
    > an integer value(decimal) and then after converting it into
    > hexadecimal value i need to write it into a file.


    When Richard said "numbers don't have bases", it means that there is no
    such thing as a hexadecimal value.

    > i do not need to
    > print it..so using fprintf along with %lx would not help me.for eg..if
    > i have a decimal value of 60 to be passed to a function ..i need that
    > function to convert it into hexadecimal value(eg 3c) and then write it
    > into a file


    Formatting a number in hexadecimal and writing to a file is what fprintf
    will do, using a %lx format specifier.

    --
    Thad
     
    Thad Smith, Sep 2, 2007
    #6
  7. writes:
    [...]

    Please don't quote signatures (the part of the article following the
    "-- " delimiter).

    > Im sry may be i didnot frame the question properly..i need to accept
    > an integer value(decimal) and then after converting it into
    > hexadecimal value i need to write it into a file.i do not need to
    > print it..so using fprintf along with %lx would not help me.for eg..if
    > i have a decimal value of 60 to be passed to a function ..i need that
    > function to convert it into hexadecimal value(eg 3c) and then write it
    > into a file


    When you say you want to write the hexadecimal value 3c to a file, do
    you mean that you want the file to contain the characters '3' and 'c',
    or do you want the actual raw non-textual value written to a file?

    The latter is referred to as "binary", not hexadecimal. The term
    hexadecimal refers only to a textual representation that uses the
    digits '0'..'9' and the letters 'a'..'f' (or 'A'..'F'). (Binary data
    is often displayed in hexadecimal, which leads some people to think
    that it *is* hexadecimal, but it isn't; the process of displaying it
    requires a conversion from one form to another.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,981
    Anders Hellerup Madsen
    Feb 23, 2004
  2. muss
    Replies:
    13
    Views:
    729
    Vladimir S. Oka
    Mar 27, 2006
  3. Replies:
    14
    Views:
    1,069
    Simon Biber
    Nov 16, 2006
  4. Replies:
    6
    Views:
    521
    Barry Schwarz
    Sep 4, 2007
  5. Bob Sanders
    Replies:
    5
    Views:
    243
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page