Solved

About Manipulating Binary File

Posted on 2006-10-22
7
260 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
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 250 total points
ID: 17784228
printf("0x%04x", mf[i]);
0
 
LVL 9

Author Comment

by:william007
ID: 17794379
Thanks=)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

910 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

22 Experts available now in Live!

Get 1:1 Help Now