i am trying to read from a binary file.

hexcode of the binary:
ff d8 ff d2 44 54 39 02 23 03 05

Code:
char buf[20];
fread(buffer,sizeof(char),4,in);
buf[0]=buf[0]&0xff;
buf[1]=buf[1]&0xff;
printf(" %x %x",buf[0],buf[1]);
half the times i get this as teh out put:
ff d8
rest of teh times :
ffffffff ffffffd8

compiler i am using is dmc;

i want the output to be consistently ff d8 all of the times.what do i need to do for that?