Question

I need to convert a ByteString to a Float32 (Exactly a 32-bit big-endian IEEE 754 floating point number). The ByteString is a part of an open sound control stream, received via UDP client. I've spent a lot of time researching, so I'd like of someone handy with Smalltalk could give me a solution.

Thanks, in advance.

Was it helpful?

Solution

Since you seem to be receiving binary data, and not a decimal number in formatted ASCII, I would not recommend to call it ByteString, but rather ByteArray, Strings are an abstraction for containing characters, not bits.

In the case of VisualWorks, there is a class called UninterpretedBytes specialized in storing raw data (bits or rather bytes) for later interpretation. This class has all the message you need to interpret the bytes, like for example #floatAt:bigEndian:

| yourBinaryStream buffer |
yourBinaryStream := ... insert some code to create your stream here...
buffer:= UninterpretedBytes from: (yourBinaryStream next: 4).
nextFloat := buffer floatAt: 1 bigEndian: true

OTHER TIPS

In Pharo Smalltalk you can do:

   (Float readFrom: '4.2') asIEEE32BitWord

readFrom: just reads a float from a string, and then you convert it to IEEE 754...

In VisualWorks you need to use the superclass method readFrom: as implemented in class Number. First create a readstream on the string, for example:

Number readFrom: '192843.887' readStream
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top