[squeak-dev] 32 bits single precision float number to a byte array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] 32 bits single precision float number to a byte array

SergeStinckwich
Hi all,

i'm looking for a way to convert a float (seen as a 32 bits single
precision float number) as a byte array of length 4 ?

Thank you,
--
Serge Stinckwich
UMI UMMISCO 209 (IRD/UPMC), Hanoi, Vietnam
Smalltalkers do: [:it | All with: Class, (And love: it)]
http://blog.doesnotunderstand.org/

Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: 32 bits single precision float number to a byte array

Andreas.Raab
Serge Stinckwich wrote:
> i'm looking for a way to convert a float (seen as a 32 bits single
> precision float number) as a byte array of length 4 ?

Given that there is no builtin 32bit single precision float type in
Squeak, how is this float stored? If it's in a float array you can just
get the bits out of it using #basicAt: and store that. E.g.,

   fltArray := FloatArray with: 42.0.
   word := fltArray basicAt: 1.
   bytes := (ByteArray new: 4) unsignedLongAt: 1 put: word; yourself.

Cheers,
   - Andreas

Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: 32 bits single precision float number to a byte array

SergeStinckwich
Andreas Raab a écrit :

> Serge Stinckwich wrote:
>> i'm looking for a way to convert a float (seen as a 32 bits single
>> precision float number) as a byte array of length 4 ?
>
> Given that there is no builtin 32bit single precision float type in
> Squeak, how is this float stored? If it's in a float array you can just
> get the bits out of it using #basicAt: and store that. E.g.,
>
>   fltArray := FloatArray with: 42.0.
>   word := fltArray basicAt: 1.
>   bytes := (ByteArray new: 4) unsignedLongAt: 1 put: word; yourself.
>

There is a primitive failed in ByteArray>>integerAt:put:size:signed with
  your solution ...

Seems to work if i add bigEndian:true :

fltArray := FloatArray with: 42.0.
word := fltArray basicAt: 1.
bytes := (ByteArray new: 4) unsignedLongAt: 1 put: word bigEndian:true;
yourself.

Cheers,
--
Serge Stinckwich
UMI UMMISCO 209 (IRD/UPMC), Hanoi, Vietnam
Smalltalkers do: [:it | All with: Class, (And love: it)]
http://doesnotunderstand.org/


Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: 32 bits single precision float number to a byte array

Andreas.Raab
Serge Stinckwich wrote:
> There is a primitive failed in ByteArray>>integerAt:put:size:signed with
>  your solution ...

Ah, yes. This is the platform specific access for FFI and friends.

> Seems to work if i add bigEndian:true :

You're right. That is the correct solution.

Cheers,
   - Andreas

> fltArray := FloatArray with: 42.0.
> word := fltArray basicAt: 1.
> bytes := (ByteArray new: 4) unsignedLongAt: 1 put: word bigEndian:true;
> yourself.
>
> Cheers,


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: 32 bits single precision float number to a byte array

David T. Lewis
On Thu, Feb 26, 2009 at 01:15:36AM -0800, Andreas Raab wrote:

> Serge Stinckwich wrote:
> >There is a primitive failed in ByteArray>>integerAt:put:size:signed with
> > your solution ...
>
> Ah, yes. This is the platform specific access for FFI and friends.
>
> >Seems to work if i add bigEndian:true :
>
> You're right. That is the correct solution.
>
> Cheers,
>   - Andreas
>
> >fltArray := FloatArray with: 42.0.
> >word := fltArray basicAt: 1.
> >bytes := (ByteArray new: 4) unsignedLongAt: 1 put: word bigEndian:true;
> >yourself.

Note also that this does not work on a 64 bit host (e.g. 64 bit Linux).

Dave