Solved

writing to file in hex

Posted on 2000-04-10
12
248 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft vcsetup.exe and vcssetup.exe 8 421
reading tzdatabase for timezone definitions 5 133
how to understand recursion 12 224
Autosar OS Multicore Share Resources confusion ? 2 35
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now