D
David Buchan
Hi guys,
This may be a dumb question; I'm just getting into C language here.
I wrote a program to unpack a binary file and write out the contents to
a new file as a list of unsigned integers. It works on an IBM mainframe
under AIX with GCC, but has trouble on Intel with GCC (DJGPP). I think
it's an endian problem.
The trouble is, although most values in the extracted list are correct,
I know of at least two values that are definitely wrong. They should be
249 and I get 4294967289 (or -7, depending on whether I have the output
to unsigned integer or just integer, respectively).
Anyway, it sure looks like an endian mixup to me.
The extract program...
#include <stdio.h>
int main ()
{
int fdi, n;
char buf[1];
FILE *fdo, *fopen();
/* Open binary bitmap file */
fdi=open("output.bmp", 0);
if (fdi==-1) {
printf ("Can't open bitmap file.\n");
exit (1);
}
/* Open decimal output file */
fdo=fopen("output.dec", "w");
/* Copy contents of file */
while ((n=read (fdi, buf, 1))>0) {
fprintf (fdo, "%u\n", (unsigned int) buf[0]);
}
/* Close all files */
close (fdi);
close (fdo);
}
Any idea what to do on Intel to make it work?
Thanks,
Dave
This may be a dumb question; I'm just getting into C language here.
I wrote a program to unpack a binary file and write out the contents to
a new file as a list of unsigned integers. It works on an IBM mainframe
under AIX with GCC, but has trouble on Intel with GCC (DJGPP). I think
it's an endian problem.
The trouble is, although most values in the extracted list are correct,
I know of at least two values that are definitely wrong. They should be
249 and I get 4294967289 (or -7, depending on whether I have the output
to unsigned integer or just integer, respectively).
Anyway, it sure looks like an endian mixup to me.
The extract program...
#include <stdio.h>
int main ()
{
int fdi, n;
char buf[1];
FILE *fdo, *fopen();
/* Open binary bitmap file */
fdi=open("output.bmp", 0);
if (fdi==-1) {
printf ("Can't open bitmap file.\n");
exit (1);
}
/* Open decimal output file */
fdo=fopen("output.dec", "w");
/* Copy contents of file */
while ((n=read (fdi, buf, 1))>0) {
fprintf (fdo, "%u\n", (unsigned int) buf[0]);
}
/* Close all files */
close (fdi);
close (fdo);
}
Any idea what to do on Intel to make it work?
Thanks,
Dave