We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

on
Medium Priority
345 Views
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
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?
}

Comment
Watch Question

## View Solutions Only

CERTIFIED EXPERT
Top Expert 2006
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
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?
CERTIFIED EXPERT
Top Expert 2006

Commented:
Use %x and not %p as format specifier in printf ...

Commented:
Hi sunnycoder, I have used the %x, I am using gcc in Linux FC 5.
CERTIFIED EXPERT
Top Expert 2006

Commented:
hmmm ... use unsigned char * mf instead of char * mf
Commented:
printf("0x%04x", mf[i]);

Commented:
Thanks=)
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile