Solved

About Manipulating Binary File

Posted on 2006-10-22
7
240 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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Use %x and not %p as format specifier in printf ...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Author Comment

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

Expert Comment

by:sunnycoder
Comment Utility
hmmm ... use unsigned char * mf instead of char * mf
0
 
LVL 5

Assisted Solution

by:daohailam
daohailam earned 250 total points
Comment Utility
printf("0x%04x", mf[i]);
0
 
LVL 9

Author Comment

by:william007
Comment Utility
Thanks=)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now