Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:22:13+00:00 2026-06-03T03:22:13+00:00

Are there any equivalents of java floatToRawIntBits/intBitsToFloat (or doubleToRawLongBits/longBitsToDouble) in ruby?

  • 0

Are there any equivalents of java floatToRawIntBits/intBitsToFloat (or doubleToRawLongBits/longBitsToDouble) in ruby?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:22:14+00:00Added an answer on June 3, 2026 at 3:22 am

    While there is not a simple alternative such as a floatToRawIntBits function, you could use Array#packand String#unpack to obtain the underlying bits:

    bytes = [3.14].pack('D').each_byte.map { |byte| byte.ord.to_s(2) }
    => ["11111", "10000101", "11101011", "1010001", "10111000", "11110", "1001",
        "1000000"]
    
    bytes.map { |byte| byte.to_i(2).chr }.join.unpack('D')
    => [3.14]
    

    What’s going on here it’s you’re packing an array (we’re using an array with a single element, so we’re really packing just the element, i.e. 3.14) to a string that represents each byte with an ASCII character.
    We then take each byte of the string (it’s the same as each character, because we’re using ASCII characters) and take the String#ord of the character; in Ruby 1.9 characters are strings of a single element, and String#ord returns the ordinal of a single character string.
    Using Integer#to_s, with 2 as an argument, we get a binary representation of the ordinal, which is what we were searching for, the single bits of every byte of our float.

    Note that passing 'D' to Array#pack means we want the bytes (what we get is characters in a string, but the method is really low-level enough to think in terms of bytes) that represent the element of the array as a double in native machine format. It’s important to pass the same parameter to String#unpack when getting back the number, because bytes are just bytes: the method doesn’t know anything about endianess, or about the data type.

    Check the docs for Array#pack and String#unpack for more info (especially regarding the correct byte order).

    Edit: You can also get the underlying bits in a string form:

    bytes = [3.14].pack('D').each_byte.
        map { |byte| ('0' * 7 + byte.ord.to_s(2))[-8,8] }
     => ["00011111", "10000101", "11101011", "01010001", "10111000", "00011110",
         "00001001", "01000000"] 
    
    bits = bytes.join(' ')
    => "00011111 10000101 11101011 01010001 10111000 00011110 00001001 01000000"
    
    bits.gsub(' ', '')
    => "0001111110000101111010110101000110111000000111100000100101000000"
    
    bits.split(' ').map { |byte| byte.to_i(2).chr }.join.unpack('D')
    => [3.14]
    

    Edit 2: If you have some string read from a file, you can try with:

    # You'll probably read each_line from a file, and get a bunch of strings like:
    from_file =  0001111110000101111010110101000110111000000111100000100101000000
    
    # If you've no byte separator in your file, the trick is to scan the string
    # for 8 digits groups, that is bytes:
    bytes = from_file.scan(/\d{8}/)
    =>["00011111", "10000101", "11101011", "01010001", "10111000", "00011110",
       "00001001", "01000000"]
    
    # Then, we can go on unpacking:
    bytes.map { |byte| byte.to_i(2).chr }.join.unpack('D')
    => [3.14]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
I am looking for a Java equivalent to .NET's Snippet Compiler. Is there any
Are there any PHP equivalents for these two functions? I tried searching but couldn't
Is there any equivalent of Java's invokeLater() method of SwingUtilities in Javascript? UPDATE 1
Is there any equivalent of Reactive Extensions (.NET) for Java? About Rx (Reactive Extensions)
Is there a Java equivalent of SQL's COALESCE function? That is, is there any
Is there any tag in Struts1 tag library which can format a java.util.Date object?
I'm new to Windows Forms from the Java Swing world. Is there any equivalent
Is there any library equivalent to the Java Robot class in OSX/Cocoa? Thanks
Are there any open-source tools available which support searching for Java methods by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.