convert decimal to hexadecimal number

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

  1. Guest

    im coding in c....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
    #1
    1. Advertising

  2. >>>>> "sa" == sweeet addiction16 <> writes:

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

    fprintf will help; read its documentation.
    Dale Henderson, Sep 2, 2007
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    >
    > im coding in c....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


    fprintf does what you want, that is, if you want to write the "text
    representation" of the value. If you simply want to write the value to the
    file, in binary mode, as a series of bytes, use fwrite. Be aware that
    non-text data might not be portable across systems.
    santosh, Sep 2, 2007
    #3
  4. Army1987 Guest

    On Mon, 03 Sep 2007 01:23:49 +0530, santosh wrote:

    > wrote:
    >
    >>
    >> im coding in c....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

    >
    > fprintf does what you want, that is, if you want to write the "text
    > representation" of the value. If you simply want to write the value to the
    > file, in binary mode, as a series of bytes, use fwrite. Be aware that
    > non-text data might not be portable across systems.

    Huh?
    fwrite("3c", 1, 2, stream) does the same thing as fprintf(stream,
    "%x", 60U) (except for the type of the result, and for its value
    on failure).
    Maybe what you mean is putc(60, stream), but if the OP meant this
    it is not very likely that he asked about conversion.

    Both fwrite() and fprintf() are implemented "as by a sequence of
    putc()", the only difference is whether the file is opened as
    text or as binary. Indeed, it is very easy to imagine a situation
    where you use them both on the same file <ot>(e.g. to create a
    ppm image)</ot>.
    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
    Army1987, Sep 3, 2007
    #4
  5. On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <>
    wrote:

    >On Mon, 03 Sep 2007 01:23:49 +0530, santosh wrote:
    >
    >> wrote:
    >>
    >>>
    >>> im coding in c....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

    >>
    >> fprintf does what you want, that is, if you want to write the "text
    >> representation" of the value. If you simply want to write the value to the
    >> file, in binary mode, as a series of bytes, use fwrite. Be aware that
    >> non-text data might not be portable across systems.

    >Huh?
    >fwrite("3c", 1, 2, stream) does the same thing as fprintf(stream,
    >"%x", 60U) (except for the type of the result, and for its value
    >on failure).
    >Maybe what you mean is putc(60, stream), but if the OP meant this
    >it is not very likely that he asked about conversion.
    >
    >Both fwrite() and fprintf() are implemented "as by a sequence of
    >putc()", the only difference is whether the file is opened as
    >text or as binary. Indeed, it is very easy to imagine a situation


    Neither fwrite nor fprintf care whether the file was opened as text or
    binary. On some systems, the underlying I/O package may care. For
    example, Windows will expand a \n to \r\n in a text file but not a
    binary one. Other systems, such as Unix, don't care at all.

    >where you use them both on the same file <ot>(e.g. to create a
    >ppm image)</ot>.



    Remove del for email
    Barry Schwarz, Sep 3, 2007
    #5
  6. Army1987 Guest

    On Mon, 03 Sep 2007 10:02:56 -0700, Barry Schwarz wrote:

    > On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <>
    > wrote:

    [snip]
    >>fwrite("3c", 1, 2, stream) does the same thing as fprintf(stream,
    >>"%x", 60U) (except for the type of the result, and for its value on
    >>failure).

    [snip]
    >>Both fwrite() and fprintf() are implemented "as by a sequence of
    >>putc()", the only difference is whether the file is opened as text or as
    >>binary. Indeed, it is very easy to imagine a situation

    >
    > Neither fwrite nor fprintf care whether the file was opened as text or
    > binary. On some systems, the underlying I/O package may care. For
    > example, Windows will expand a \n to \r\n in a text file but not a
    > binary one. Other systems, such as Unix, don't care at all.

    That's what I was trying to mean. Sorry if I wasn't clear. I was
    just saying that both fwrite() and fprintf() (appear to) produce
    output as a sequence of putc(), and what putc() does, from a
    certain POV, depends on whether the stream is text or binary.
    --
    Army1987 (Replace "NOSPAM" with "email")
    No-one ever won a game by resigning. -- S. Tartakower
    Army1987, Sep 3, 2007
    #6
  7. On Mon, 03 Sep 2007 20:29:29 +0200, Army1987 <>
    wrote:

    >On Mon, 03 Sep 2007 10:02:56 -0700, Barry Schwarz wrote:
    >
    >> On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <>
    >> wrote:

    >[snip]
    >>>fwrite("3c", 1, 2, stream) does the same thing as fprintf(stream,
    >>>"%x", 60U) (except for the type of the result, and for its value on
    >>>failure).

    >[snip]
    >>>Both fwrite() and fprintf() are implemented "as by a sequence of
    >>>putc()", the only difference is whether the file is opened as text or as
    >>>binary. Indeed, it is very easy to imagine a situation

    >>
    >> Neither fwrite nor fprintf care whether the file was opened as text or
    >> binary. On some systems, the underlying I/O package may care. For
    >> example, Windows will expand a \n to \r\n in a text file but not a
    >> binary one. Other systems, such as Unix, don't care at all.

    >That's what I was trying to mean. Sorry if I wasn't clear. I was
    >just saying that both fwrite() and fprintf() (appear to) produce
    >output as a sequence of putc(), and what putc() does, from a
    >certain POV, depends on whether the stream is text or binary.


    Only on those systems where there is a distinction. On other systems,
    putc() does the same to both, regardless of the mode of the stream.


    Remove del for email
    Barry Schwarz, Sep 4, 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,763
    Anders Hellerup Madsen
    Feb 23, 2004
  2. muss
    Replies:
    13
    Views:
    714
    Vladimir S. Oka
    Mar 27, 2006
  3. Replies:
    14
    Views:
    1,033
    Simon Biber
    Nov 16, 2006
  4. Replies:
    6
    Views:
    616
    Keith Thompson
    Sep 2, 2007
  5. Bob Sanders
    Replies:
    5
    Views:
    216
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page