Lazarus

Programming => General => Topic started by: andresayang on March 14, 2018, 09:36:01 am

Title: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: andresayang on March 14, 2018, 09:36:01 am
Hi all,

Well the title says everything ... I need to write a 32Bit IEEE float (single) to a TMemoryStream, How to do that ?
Or Is there any function which transform a Single to a QWord ?

Thanks a lot,

Andre

Ok, sorry, I can not delete my post si I think I find it, using TSingleHelper should do the job.
Title: Re: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: Thaddy on March 14, 2018, 10:02:02 am
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses classes;
  3. var
  4.   Stream:TMemoryStream;
  5.   S:Single = 0.0001;
  6. begin
  7.   Stream := TMemoryStream.Create;
  8.   try
  9.     Stream.Write(S, SizeOf(S));
  10.     Stream.Position := 0;  // reset stream;
  11.     S:= 0.0; //reset S;
  12.     Stream.Read(S, SizeOf(S)); // Read it back
  13.     Writeln(S :1:4); // Check if it worked
  14.   finally
  15.     Stream.Free; // Release the stream;
  16.   end;
  17. end.
Title: Re: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: Thaddy on March 14, 2018, 10:10:51 am
Ok, sorry, I can not delete my post si I think I find it, using TSingleHelper should do the job.
Not necessary as per my example 8-) It is as simple as that.
Title: Re: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: andresayang on March 14, 2018, 12:25:05 pm
Thanks a lot,

Your example is really simple !

And tested, got expected result, thanks.


Cheers
Title: Re: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: Bart on March 14, 2018, 04:48:18 pm
Ok, sorry, I can not delete my post si I think I find it, using TSingleHelper should do the job.

TFloatHelpers are broken, see issue #32837 (https://bugs.freepascal.org/view.php?id=32837).

Bart
Title: Re: How to write Single (ie float 32 bit) in a TMemoryStream
Post by: Thaddy on March 14, 2018, 06:15:55 pm
TFloatHelpers are broken, see issue #32837 (https://bugs.freepascal.org/view.php?id=32837).

Bart
That's why I did not use them in my example... O:-)

(Others plz note they are not really broken...but Bart has a point in that bug report)
TinyPortal © 2005-2018