I have simple C and Java code that reads an integer from a non-text file. When I print out the result in hexadecimal, it looks like the byte order for the Java code is different than the C code. Example:
int fd = open("MyFile", O_RDONLY);
int rc = read(fd, &MagicNumber, 4);
printf("MagicNumber is 0x%x\n", MagicNumber);
This prints: MagicNumber is 0xff017ffe
The Java code is:
FileInputStream fin = new FileInputStream("MyFile");
DataInputStream din = new DataInputStream(fin);
int MagicNumber = din.readInt();
System.out.printf("MagicNumber is 0x%x\n", MagicNumber);
It prints: MagicNumber is 0xfe7f01ff
The output is similar, but the bytes are swapped. Am I doing file I/O incorrectly? Do I need to do something to read bytes consistently between C and Java?