convert decimal to hexadecimal number

  • Thread starter sweeet_addiction16
  • Start date
S

sweeet_addiction16

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
 
D

Dale Henderson

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

santosh

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

Army1987

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

Barry Schwarz

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
 
A

Army1987

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

Barry Schwarz

[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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top