smalltalkvisualworks

Converting in Smalltalk VisualWorks 7.9.1


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.


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