# convert decimal to hexadecimal number - C

This is a discussion on convert decimal to hexadecimal number - C ; 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 ...

1. ## convert decimal to hexadecimal number

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

2. ## Re: convert decimal to hexadecimal number

>>>>> "sa" == sweeet addiction16 <sweeet_addiction16@yahoo.com> 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.

3. ## Re: convert decimal to hexadecimal number

>
> 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.

4. ## Re: convert decimal to hexadecimal number

On Mon, 03 Sep 2007 01:23:49 +0530, santosh 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

5. ## Re: convert decimal to hexadecimal number

On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <army1987@NOSPAM.it>
wrote:

>On Mon, 03 Sep 2007 01:23:49 +0530, santosh 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

6. ## Re: convert decimal to hexadecimal number

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

> On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <army1987@NOSPAM.it>
> 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

7. ## Re: convert decimal to hexadecimal number

On Mon, 03 Sep 2007 20:29:29 +0200, Army1987 <army1987@NOSPAM.it>
wrote:

>On Mon, 03 Sep 2007 10:02:56 -0700, Barry Schwarz wrote:
>
>> On Mon, 03 Sep 2007 12:41:45 +0200, Army1987 <army1987@NOSPAM.it>
>> 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