I have an IOStream and I want to read a series of floats that are stored in big-endian order, while my computer is usually little-endian. So there are a few parts to my question:
- If I know that the computer is little-endian, can I read the float like this?:
value = bswap(read(io,Float32))
My thinking is that
read(io,Float32) will at least give me the correct set of bytes, and
bswap() seems to preserve the type.
- What I wrote above is fine for a small file, but I eventually want to read several GiB of data. Is there a way to read many floats and then convert them all to little-endian? I know you can read all the bytes with
bytes = read(io,sizeof(Float32)*ncells)
But I don’t know what to do with those bytes once I have them? How do I put them into an array of floats?
- To make my code more portable, it would be nice if it could test endianness. How do I determine whether the computer I’m currently running on is little-endian or big-endian?