Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-10

How to read binary file created by fortran or octave?

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

mili

Hello, every body

I try to use fpc to read some binary data files created by fortran and octave files. I found that TFileStream.Read() can read out integer (int32) data correctly, but can't read out single (float32) data correctly. All float data is wierd. So can anybody tell me how to correctly use pascal to read them?

Thanks,
Yours,
mili

Unknown author
Show quoted text 7 lines
Hello, every body I try to use fpc to read some binary data files created by fortran and octave files. I found that TFileStream.Read() can read out integer (int32) data correctly, but can't read out single (float32) data correctly. All float data is wierd. So can anybody tell me how to correctly use pascal to read them?

What format is the float32 data in? You say 'single', but do you mean 'single' as in the IEEE format, or another format?

The IEEE Single format is fixed, and should follow this format:

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
0 1 8 9 31

X = 1^S * 2^e-bias * 1.f

Basically, the underlying memory is a 32bit unsigned integer (LongWord.)

If it is IEEE Single format then if you are having problems you could try reading the data using the following format:

type
  TSingleFormat = Array[1..4] of byte;
  TSingleOverlay = record
    case word of
      1: (Float: single);
      2: (ByteArr: TSingleFormat);
      3: (Integer: LongWord);
  end;

So long as the host system is the same Endian, the IEEE format will be correctly held. If the Endianess of the system is not compatible with the system that wrote the data, the bytes will need to be reversed:

var
  x: TSingleOverlay;
..
  x.Float := 10.03;
  {x.ByteArr[1] = $E2
   x.ByteArr[2] = $7A
   x.ByteArr[3] = $20
   x.ByteArr[4] = $41
  }

If you can verify this, you can use this technique to convert the single. If you find the values are reversed, e.g.

{x.ByteArr[4] = $E2
x.ByteArr[3] = $7A
x.ByteArr[2] = $20
x.ByteArr[1] = $41
}

You must swap the bytes to make the correct endianess.

Also have a look at:

http://www.psc.edu/general/software/packages/ieee/ieee.html http://community.borland.com/article/0,1410,15855,00.html

Hope that helps,

Matt

---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/