Re:please help to dump a real number to a file as binary data
Quote
Jono wrote:
> Hi all,
> I am a C programmer and I need to convert/read some data that seems to
> have been created with Pascal and some where in the data I think are
> real numbers in binary format. In order for me to be able to test my
> conversion routine I would like to be able to create a Delphi program
> to dump real numbers to a file. I would be so grateful if some one
> could show me how to do that, thanks. The Delphi/Pascal program/code
> that I need in C terms would basically be like:
Okie. Well, just a few quick questions.
1. At the last count there are four floating point formats that may be
used in Delphi. Any idea which? If you can give us an idea of the size
in bytes, then we can hazard a quess.
2. Three of those fornats are standard not only amongst all intel
machines, but are also compliant with the IEEE floating point standards.
If you know it's a float, and you know what size it is, your C program
should be able to read it directly. If you program in C on a big endian
machine, such as a Sun workstation, then reverse the byte order, and
just read it straight into your beloved FPU.
Anyway... perhaps something like this.
BTW. I don't guarantee this doesn't have any syntax errors!
It might just work under delphi 2.
Please excuse my using the file stream object, but I just can't remember
the old pascal file handling functions.
program floattest;
{$APPTYPE CONSOLE}
uses Classes;
var
float0:real; {pascal specific, 6 bytes}
float1:single; {IEEE standard 4 bytes}
float2:double; {IEEE standard 8 bytes}
float3:extended; {IEEE standard 10 bytes}
Out:TFileStream;
begin
float0:=23.56;
float1:=23.56;
float2:=23.56;
float3:=23.56;
Out:=TFileStream.Create('c:\test.bin',fmCreate);
try
with Out do
begin
WriteBuffer(@float0,SizeOf(float0));
WriteBuffer(@float1,SizeOf(float1));
WriteBuffer(@float2,SizeOf(float2));
WriteBuffer(@float3,SizeOf(float3));
end;
finally
Out.Free;
end;
end;
HTH. Martin H.