How to judge the platform is little or big endian ?
I have a c code, but anybody has a Java code?
Thanks.
#include <stdio.h>
//little-endian or big-endian
bool IsLittleEndian()
{
int i = 1;
char* p = (char*)&i;
return *p;
}
int main()
{
if(IsLittleEndian())
printf("LittleEndian\n");
else
printf("BigEndian\n");
return 0;
}
[775 byte] By [
ardmorea] at [2007-11-27 10:16:51]

Java is platform independent, and always big-endian.
I am reading a binary file created from Fortran.
> I am reading a binary file created from Fortran.
Does Fortran write the file big or little endian?
> I am reading a binary file created from Fortran.
Then you have to find out
* Whether Fortran is always big/lttle.
* If platform-dependent, what the endianness was for the platform where it was created.
jverda at 2007-7-28 15:48:03 >

If you want to take a stream of bytes written in one order and read them in the other order, you can use the java.nio.Buffers to do this, or roll up your sleeves and do it yourself.
One more question.
It appears that the data I/O methods for the RandomAccessFile class
(e.g. readDouble() and writeDouble()) handle data in "big endian format"
(most significant byte first). If you have a file where the data was
written in "little endian format" (least significant byte first" how
is the best way to read that data in Java?
Does Fortran write the file big or little endian?
This binary file is written by Fortran in another machine. So I have to determine whether it is a big or a little endian format. I google the web but no answer.
Any advice will be appreciated.
Thanks.
> Any advice will be appreciated.
Get a sample.
> Does Fortran write the file big or little
> endian?
> This binary file is written by Fortran in another
> machine. So I have to determine whether it is a big
> or a little endian format. I google the web but no
> answer.
>
>
> Any advice will be appreciated.
> Thanks.
That doesn't matter if it's done on another machine. A file's a file. You need to know which way the file's written if you want to know how to read it. For instance, if you have the following bytes in the file:
A3 4F
How can you tell if that's big or little endian? You can't unless you know something about that data in the file, which you probably don't until you can read it with the proper endianness, etc.
Is the file written manually by a Fortran process, or is it a standard Fortran method that writes it? If it's a documented Fortran feature, you should be able to find out which it is. If it's manually written, then you need to find out which direction it's written.
Either way, you need to know how it was written, you can't figure that out just from the file data.
15 45
4 0.000000000000000E+000 0.000000000000000E+000
0.000000000000000E+000 0.2100000000000000.210000000000000
0.2100000000000000.2100000000000000.000000000000000E+000
4 0.6300000000000000.000000000000000E+000
0.4860801862745820.000000000000000E+000 0.486080186274582
0.000000000000000E+000 0.6300000000000000.210000000000000
4 0.6300000000000000.630000000000000
0.6300000000000000.4200000000000000.630000000000000
0.4200000000000000.4200000000000000.420000000000000
4 0.000000000000000E+000 0.630000000000000
0.2100000000000000.6300000000000000.210000000000000
0.4298108558640460.2100000000000000.429810855864046
5 0.2100000000000000.000000000000000E+000
0.2100000000000000.000000000000000E+000 0.362436069424948
0.2100000000000000.3629099259729040.209526143452043
0.4190522869040870.000000000000000E+000
...
standard Fortran method :fortran 90
if(.not. allocated(a)) then
allocate ( a(numlines) )
call opnfil(b,'old','unformatted','savedata')
read(b)a
close (b)
http://www.codeproject.com/cpp/endianness.asp
I got one, but how to translate into Java code.
Auto detecting the correct Endian format of a data file
Suppose you are developing a Windows application that imports Nuclear Magnetic Resonance (NMR) spectra. High resolution NMR files are generally recorded in Silicon or Sun Workstations but recently Windows or Linux based spectrometers are emerging as practical substitutes. It turns out that you will need to know in advance the Endian format of the file to parse correctly all the information. Here are some practical guidelines you can follow to decipher the correct Endianness of a data file:
Typically, the binary file includes a header with the information about the Endian format.
If the header is not present , you can guess the Endian format if you know the native format of the computer from which the file comes from. For instance, if the file was created in a Sun Workstation, the Endian format will most likely be Big-Endian.
If none of the above points apply, the Endian format can be determined by trial and error. For example, if after reading the file assuming one format, the spectrum does not make sense, you will know that you have to use the other format.
If the data points in the file are in floating point format (double), then the _isnan() function can be of some help to determine the Endian format. For example:
double dValue;
FILE* fp;
(...)
fread(&nValue, 1, sizeof(double), fp);
bool bByteSwap = _isnan(dValue) ? true : false
Note that this method does only guarantee that the byte swap operation is required if _isnan() returns a nonzero value (TRUE); if it returns 0 (FALSE), then it is not possible to know the correct Endian format without further information.
