?
Solved

About Manipulating Binary File

Posted on 2006-10-22
7
Medium Priority
?
334 Views
Last Modified: 2010-04-15
myFile.txt is a binary file:

Followed is the result of od -Ax -tx1 myFile.txt:
000000 68 01 00 00 a0 05 00 00 48 00 00 00 38 05 00 00
000010 53 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00

Followed is the code
//myFile.txt is being memory map
            char * mf=mmap(0,n,PROT_READ,MAP_PRIVATE,f,0);
               int decArray=decArray[16];
            int i;
            for (i=0;i<16;i++){
                    //1. How to get the value of  mf[i], and print it on the screen, all in the format of 0x00**, eg 0x0068, 0x00a0?
                    //2. How to convert mf[i] in the decimal format and store it in the decArray?
            }

0
Comment
Question by:william007
  • 3
  • 3
7 Comments
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 total points
ID: 17783289
1.
printf ("%x", mf[i] );

2.
You should be aware of what format the data is in ... mf is char array so data would be read a byte(char) at a time ... You can also cast the pointer returned by mmap to an int * and read it as ints ...

If you need the previous data of step to be converted to ints, use
decArray[i] = mf[i];

Note that no conversion in required since chars are internally stored as ints ... The difference between this method and casting mf to int * is that here each char (byte) is being assigned to an int while in latter 4 consecutive bytes form an int
0
 
LVL 9

Author Comment

by:william007
ID: 17783331
Hi for 1, the result is like this
68
1
0
0
ffffffa0
5
....
But what I want is
0x0068
0x0001
0x0000
0x0000
0x00a0
0x0005
is it possible?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17783415
Use %x and not %p as format specifier in printf ...
0
Independent Software Vendors: 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!

 
LVL 9

Author Comment

by:william007
ID: 17783428
Hi sunnycoder, I have used the %x, I am using gcc in Linux FC 5.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17783435
hmmm ... use unsigned char * mf instead of char * mf
0
 
LVL 5

Assisted Solution

by:daohailam
daohailam earned 1000 total points
ID: 17784228
printf("0x%04x", mf[i]);
0
 
LVL 9

Author Comment

by:william007
ID: 17794379
Thanks=)
0

Featured Post

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!

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use switch statements in the C programming language.
Suggested Courses

839 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