Converting 32-bit Single Precision Floating Point Number to Decimal

aheadbehind's icon

I'm writing a Max patch that receives via the serial object a stream of 32-bit floating point single precision numbers (from a 3D accelerometer/gyroscope device made by Yost Engineering, called the 3-Space Sensor). The incoming data comes in packets that look like this: 62 165 14 56, 62 142 208 54, etc. Each number in each group of 4 numbers is the decimal version of a hex number that represents one of the 4 bytes in the 32-bit float, so the first packet I just gave (62 165 14 56) is the same as 0x3E 0xA5 0x0E 0x38, which is actually the decimal number 0.322 written in the IEEE754 single precision 32-bit format. I've learned on Wikipedia that this format represents the fraction as the first 23 bits, the exponent as the next 8 bits, and the sign as the last bit.

Here's where I'm stuck. I can't figure out how to write a Max patch that will convert the incoming stream of 32-bit single precision packets into decimal numbers that I can work with (like connect to a dial object, etc.). I've found calculators online, but those don't help as I need the math behind those calculators.

Has anyone written a Max patch to convert single precision 32-bit floating point numbers into decimal numbers that I can send to a float object? If not can anyone send me the mathematical steps required to do this? Looking online at the math, I can't figure out what to do as you have to convert to binary then grab individual bits as stated above in order to figure out what the decimal number is. But I'm lost as to how this could be done in Max. Any help will be hugely appreciated! Thanks ahead of time.

Pierre Guillot's icon
aheadbehind's icon

Thanks. This is similar, but better than the other instructions I found. However, I still can't figure out how to do this in Max. How do you "see" the bits in Max to be able to do things with them as suggested here? Is there an object that can extract bits?

Rick's icon
Max Patch
Copy patch and select New From Clipboard in Max.
metamax's icon

Here ya go... This treats lists of 4 integers as 32-bit single precision floating point values and converts them to decimal. Seems pretty accurate but I didn't test it much. Let me know if you find any bugs in it...

Max Patch
Copy patch and select New From Clipboard in Max.

aheadbehind's icon

Both approaches appear to be working. Huge thanks to both of you for the quick reply here! I'll repost if either of these patches turns up a bug, but so far so good.