storing binary in a file

Hi
i have a problem here with storing numbers in a file.
i made an array ,put in it numbers and stored them in a file using the command fwrite, but when i open the file using hexdump(i am using linux),it prints the numbers in another way.
i have here this little code


#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *RING=fopen("ring.lst","wb");
short int RI[6];
RI[0]=0;
RI[1]=1;
RI[2]=4;
RI[3]=2;
RI[4]=1;
RI[5]=14;
fwrite(RI,sizeof(short int),6,RING);

return 0;
}

when i do hexdump ring.lst it prints me this output:
0000 0001 0004 0002 0001 000e
instead 0142 1e
i have tried to do it with unsigned char which is one bit and not two like (short int) but even then it prints 01 04 02 01 0e
what should i do to get the numbers without zeros before them.


This is correct. When you read them in, they will be correct.
thanks man for answerring me, but what i meant is that i want my file to look like 0142 1e..... , but at the end i figured it out, its putting every four digits together like this

short int sum;
sum=RI[0];
sum=sum<<4;
sum=sum|RI[1];
sum=sum<<4;
sum=sum|RI[2];
sum=sum<<4;
sum=sum|RI[3];

in this way it will prints me only hex numbers because i used all of the four digits that short int gives me
OK, my bad. To do what you want, you'll need to use a character (8 bits), not a short (16 bits).
Topic archived. No new replies allowed.