3

I have a binary32 encoded in IEEE 32.

How to convert 0x0040EDC2 to -118,625 ?

I have try several options of pack and unpack without success.

IEEE : http://en.wikipedia.org/wiki/Single-precision_floating-point_format

Manu

1
  • 1
    What pack arguments did you try? How did they fail? Commented Mar 14, 2012 at 8:20

1 Answer 1

4
$ irb
irb(main):001:0> bin = "\x00\x40\xED\xC2"
=> "\000@\355\302"
irb(main):002:0> bin.unpack 'f'
=> [-118.625]
irb(main):003:0> bin.unpack 'e'
=> [-118.625]
irb(main):004:0> bin.unpack 'F'
=> [-118.625]
irb(main):005:0> i = 0x0040edc2
=> 4255170
irb(main):006:0> bin = [i].pack('L')
=> "\xC2\xED@\x00"
irb(main):007:0> bin.unpack 'g'
=> [-118.625]
irb(main):008:0> RUBY_PLATFORM
=> "x86_64-linux"
irb(main):009:0> RUBY_VERSION
=> "1.9.2"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.