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 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
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 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,
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
squeak-dev@lists.squeakfoundation.org