* * *

Author Topic: How to write Single (ie float 32 bit) in a TMemoryStream  (Read 766 times)

andresayang

  • New member
  • *
  • Posts: 32
How to write Single (ie float 32 bit) in a TMemoryStream
« 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.
« Last Edit: March 14, 2018, 09:56:15 am by andresayang »
Linux, Debian 9
Lazarus: always latest release

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: How to write Single (ie float 32 bit) in a TMemoryStream
« Reply #1 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.
« Last Edit: March 14, 2018, 10:19:12 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: How to write Single (ie float 32 bit) in a TMemoryStream
« Reply #2 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.
« Last Edit: March 14, 2018, 10:12:23 am by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

andresayang

  • New member
  • *
  • Posts: 32
Re: How to write Single (ie float 32 bit) in a TMemoryStream
« Reply #3 on: March 14, 2018, 12:25:05 pm »
Thanks a lot,

Your example is really simple !

And tested, got expected result, thanks.


Cheers
« Last Edit: March 14, 2018, 12:29:39 pm by andresayang »
Linux, Debian 9
Lazarus: always latest release

Bart

  • Hero Member
  • *****
  • Posts: 2992
    • Bart en Mariska's Webstek
Re: How to write Single (ie float 32 bit) in a TMemoryStream
« Reply #4 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.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 6152
Re: How to write Single (ie float 32 bit) in a TMemoryStream
« Reply #5 on: March 14, 2018, 06:15:55 pm »
TFloatHelpers are broken, see issue #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)
« Last Edit: March 14, 2018, 06:18:49 pm by Thaddy »
I might not give the answer that you want me to.. Peter Green 1969

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus