Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Endian 1

Status
Not open for further replies.

GerritGroot

Technical User
Nov 3, 2006
291
ES
Hi,

I've got 2 doubts, maybe you can help me.

If I read a binary file in Fortran, either with ACCESS=DIRECT giving some record length or with FORM=BINARY
1. Is there a way to make a difference between long-endian and big-endian binaries? Or in other words, if I know that my file is big-endian, how do I make sure that Fortran will read it as a big endian.

2. If the file has a mixture of ASCII and Binary in the same file, how do I read that? Because I can only open the file in one way at the time.

Thanks,

Gerrit
 
Think you meant little endian :)

1) You can make a difference by fixing the first integer that you read in. If it is always 255 and you do not get 255 then you know you have to play about with the data.

Fortran doesn't know what it is reading. You have to check the data.

2) This is a bit more complex: depends on whether the data is packed or not. If it isn't, then you just need to declare a type with the structure and read it in. You have to watch out for stuff like
Code:
type mixed
   character*1:: mOne
   integer:: mOneVal
   character*5:: mFive
   integer:: mFiveVal
end type
If it is not packed, there will probably be something between mOne and mOneVal because the integer will have to start on a word boundary. If it is packed, then the transfer function is your friend. It is the single most useful function in Fortran that can convert anything to anything. It is equivalent to a cast in C/C++. As always, just don't abuse it and you'll be fine.
 
Thank you (again) xwb,

Of course I meant little-endian with respect to big-endian (I was a bit tired when I wrote it :) )

1) Okay, writing an integer first is an idea, but you are assuming that I wrote the file myself. The binary file to be read, however, comes from another application. So, I can't assure the first integer to be 255 or 255 backwards in bits, can I?

2) The transfer function? Ehm, what's that? (No idea whether it's packed BTW, but I'll find out)

Another thing that remains unclear to me is how real values are stored in binary form. With integers it's easy (e.g. 3 in INTEGER*1 in Big-Endian "00000011"), but how are real values stored? Is there a difference between 1.1e4 and 11000.0 for example when stored in binary (let's say big-endian) form? Is there a convention of where the "." is situated as a function of the amount of bytes? E.g. "00000011.10000000" for 3.5 in 2 bytes, and is there a fixed convention about where the exponent value is stored? Like in 0.35e01 for example? (Couldn't find anything on the net about this which comes to the nitty-gritty)

Regards,

Gerrit
 
1) Yes - I did mean if you wrote it by yourself. If it comes from another app, on the same machine type or OS, the endianess will be the same. If it is a different machine type there may be a problem. eg going from PowerPC to Intel. This is purely about integers.

2a) transfer function: you'll either love it or hate it.

2b) strings - they get affected by endianess too. Eg GerritGr could be rreGfGti or eGrrtirG. Depends whether it is 2 or 4 byte.

3) The IEEE standard ( describes the storage. If the compiler/machine architecture conforms to IEEE 754, then the same sections will take the same number of bits but where they are placed in the number is a different question. They may all conform to the standard but the exponent and other bits don't have to be in the same place.
 
Thank you, especially for the link that I didn't find googling around.
 
If you are using g95, there is an endian switch to tell it how you are expecting your input data. It has 3 settings: BIG, LITTLE and NATIVE.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top