?
Solved

About Manipulating Binary File

Posted on 2006-10-22
7
Medium Priority
?
317 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
[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
  • 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
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!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
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

770 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