Richard Davison Posted August 3, 2009 Share Posted August 3, 2009 (edited) SOLVED see my next post in this thread I feel so dumb for not being able to figure this out. Does it have anything to do with the double quotations? FILE *file; file = fopen("test", "wb"); fprintf(file, "%d", 0x000000FF); fclose(file); then I call hexdump -C test and I see 00000000 32 35 35 instead of 00000000 FF 00 00 00 Edited August 3, 2009 by Richard Davison Link to comment https://www.neowin.net/forum/topic/805922-c-fprintf-writing-ascii-instead-of-binary/ Share on other sites More sharing options...
0 Lant Posted August 3, 2009 Share Posted August 3, 2009 I'm not that experience in C but aren't you writing the string "255" to the file? So the following would do what you want: fprintf(file, "%c", 0xFF); Link to comment https://www.neowin.net/forum/topic/805922-c-fprintf-writing-ascii-instead-of-binary/#findComment-591372896 Share on other sites More sharing options...
0 Pablo2008jedi Posted August 3, 2009 Share Posted August 3, 2009 http://msdn.microsoft.com/en-us/library/hf...28VS.80%29.aspx I think you use %x instead of %d Link to comment https://www.neowin.net/forum/topic/805922-c-fprintf-writing-ascii-instead-of-binary/#findComment-591372900 Share on other sites More sharing options...
0 Richard Davison Posted August 3, 2009 Author Share Posted August 3, 2009 (edited) I think I overlooked the fwrite() function. @Lant, if I just used %c, then yeah, FF would show up, but the remaining zeros would get lost. I would be writing 1 byte rather than the 4 bytes I want to write with an int. @Pabs(Sco), with %x the hexdump gives me 66 66 which the ascii equivalent to FF edit: I think I've just been using the wrong function. I should probably be using fwrite instead of fprintf edit again: this code works #include <stdio.h> void main(void) { int i = 0x000000FF; FILE *file; file = fopen("test", "wb"); fwrite(&i, sizeof(int), 1, file); fclose(file); } it spits out ff 00 00 00 good night... *yawns* Edited August 3, 2009 by Richard Davison Link to comment https://www.neowin.net/forum/topic/805922-c-fprintf-writing-ascii-instead-of-binary/#findComment-591372906 Share on other sites More sharing options...
Question
Richard Davison
SOLVED see my next post in this thread
I feel so dumb for not being able to figure this out. Does it have anything to do with the double quotations?
then I call hexdump -C test
and I see
instead of
Edited by Richard DavisonLink to comment
https://www.neowin.net/forum/topic/805922-c-fprintf-writing-ascii-instead-of-binary/Share on other sites
3 answers to this question
Recommended Posts