Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

writing to file in hex

Posted on 2000-04-10
12
Medium Priority
?
260 Views
Last Modified: 2010-04-02
I am trying to write data to a file in hex. I use the following to write to the file:

  unsigned int bfType = 0x424D;
  fprintf(output, "%X", bfType);

but it actually writes '424D' to the file as characters, so upon viewing, the hex equivalent is, '34 32 34 44' As I said, I want to write to file in hex, not as chars.. ie, write '424D' to the file, and it would come up in characters as 'BM'. How do I go about doing this correctly??

Thanks.
0
Comment
Question by:Harliquen
  • 6
  • 4
  • 2
12 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 2699710
fprintf (output,"%c%c,(bfType/256),(bfType%256));

will write BM for 0x424d
0
 
LVL 10

Expert Comment

by:rbr
ID: 2699711
Use
fprintf (output,"%c%c",(bfType/256),(bfType%256));
0
 

Author Comment

by:Harliquen
ID: 2706260
Adjusted points from 50 to 70
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Harliquen
ID: 2706261
Thanks, that does work, and I understand why.. but isn't there an easier way?? eg. what if:

 bfType = 0x424D00FFA6B21201;

instead of stripping of hex digits one at a time by /256/256/256/256.. etc can't the whole number be written directly and not necessarily using fprintf, is there another statment that will do it (points have been increased due to increase in q)??

Thanks

0
 
LVL 85

Expert Comment

by:ozo
ID: 2706282
assuming your machine representation of integers uses the same byte ordering that you want for your output, you might
fwrite( &bfType, sizeof(bftype), 1, output );
or if you want to write a constant, you can
fprintf( output,"%s","\x42\x4D\x00\xFF\xA6\xB2\x12\x01" );
0
 
LVL 10

Expert Comment

by:rbr
ID: 2706518
Ozo is correct by using fwrite. Another possibilty would be

int i;
unsigned long x=0x424db2032
char *tmp;

tmp = (char *) &x


for (i=0,i<sizeof(x);i++)
printf ("%c",*(tmp+i));
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 2706643
which has the advantage that if your machine's internal representation of integers happens to be the reverse of the byte ordering you want for output, you can change it to
for( i=sizeof(x);i-->0; )
0
 

Author Comment

by:Harliquen
ID: 2720222
Thanks ozo, your comment is alot more detailed, and is what I was after. Please post it as an answer so I can give you the points.
0
 

Author Comment

by:Harliquen
ID: 2720223
Thanks ozo, your comment is alot more detailed, and is what I was after. Please post it as an answer so I can give you the points.
0
 

Author Comment

by:Harliquen
ID: 2726513
Yes, it does work. But ozo's answer is alot more acceptable.
0
 
LVL 10

Accepted Solution

by:
rbr earned 210 total points
ID: 2726686
To Harlequin: I gave you a correct answer to your question. I gave you further information as you altered the question. So I claim the points. You could gave points to ozo too, but I gave you the correct answer.
0
 

Author Comment

by:Harliquen
ID: 2735130
feck, sorry, I didn't realise it was you that posted the further info. How do I divide the points between people?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question