Recent

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

andresayang

  • Full Member
  • ***
  • Posts: 108
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 12
Lazarus: always latest release

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
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 »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
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 »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

andresayang

  • Full Member
  • ***
  • Posts: 108
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 12
Lazarus: always latest release

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • 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: 14373
  • Sensorship about opinions does not belong here.
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 »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018