java floattorawintbits,ruby中的Java floatToRawIntBits / intBitsToFloat或doubleToRawLongBits / longBitsT...

3

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

虽然没有一个简单的替代方法,例如floatToRawIntBits函数,但您可以使用Array#pack和String#unpack来获取基础位:

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.

这里发生的是你正在打包一个数组(我们使用的是一个带有单个元素的数组,所以我们实际上只是将元素打包,即3.14)到一个字符串,该字符串用ASCII字符表示每个字节。然后我们获取字符串的每个字节(它与每个字符相同,因为我们使用的是ASCII字符)并获取字符的String#ord;在Ruby 1.9中,字符是单个元素的字符串,String#ord返回单个字符串的序数。使用Integer#to_s,以2作为参数,我们得到序数的二进制表示,这是我们要搜索的,我们浮点数的每个字节的单个位。

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.

注意,将'D'传递给Array#pack意味着我们希望字节(我们得到的是字符串中的字符,但该方法实际上是低级别,足以用字节来思考),它们表示数组的元素为原生数据格式加倍。在获取数字时,将相同的参数传递给String#unpack非常重要,因为字节只是字节:该方法不了解有关endianess或数据类型的任何信息。

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

检查有关Array#pack和String#unpack的文档以获取更多信息(特别是关于正确的字节顺序)。

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:

编辑2:如果您从文件中读取了一些字符串,则可以尝试:

# 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值