Solved

writing to file in hex

Posted on 2000-04-10
12
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 

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 84

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 84

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 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