Ruby float to Binary32
binary, floating-point, ruby
Solution
The above is awesome but I have a few simplifications:
[123.456].pack('g').bytes.map{|n| "%08b" % n}.join
Using the `'g'` flag instead of `'e'` avoids having to `reverse` the output from `pack`. The `bytes` method does the same thing as calling `.ord` on each character does. Then instead of taking the 4 integers and doing the sum/bit shift you map each to an 8 character binary string and join them together.
Problem
I've been looking all over to find if there is a method that converts a float number(ex: `123.456`) into a binary32. I've found a lot of solutions that go from binary32 to float, but not vice versa.