在Ruby中解压缩签名的little-endian

所以我正在研究一些MongoDB协议的东西。所有整数都是带小端的。使用Ruby的标准
Array#pack
方法,我可以将整数转换为我想要的二进制字符串:
positive_one = Array(1).pack('V')   #=> 'x01x00x00x00'
negative_one = Array(-1).pack('V')  #=> 'xFFxFFxFFxFF'
但是,另一方面,
String#unpack
方法将'V'格式记录为具体返回无符号整数:
positive_one.unpack('V').first #=> 1
negative_one.unpack('V').first #=> 4294967295
签名的little-endian字节顺序没有格式化器。我确信我可以使用位移来玩游戏,或者编写我自己的不使用数组打包的字节错误方法,但我想知道是否有其他人遇到过这个并找到了一个简单的解决方案。非常感谢。     
已邀请:
"V"
打开包装后,您可以应用以下转换
class Integer
  def to_signed_32bit
    if self & 0x8000_0000 == 0x8000_0000
      self - 0x1_0000_0000  
    else
      self
    end
  end
end
如果你正在处理其他大小的整数,你需要改变魔法常数
0x1_0000_0000
(即
2**32
)和
0x8000_0000
2**31
)。     
编辑我误解了你原来转换的方向(根据评论)。但在考虑了一些之后,我相信解决方案仍然是一样的。这是更新的方法。它完全相同,但评论应解释结果:
def convertLEToNative( num )
    # Convert a given 4 byte integer from little-endian to the running
    # machine's native endianess.  The pack('V') operation takes the
    # given number and converts it to little-endian (which means that
    # if the machine is little endian, no conversion occurs).  On a
    # big-endian machine, the pack('V') will swap the bytes because
    # that's what it has to do to convert from big to little endian.  
    # Since the number is already little endian, the swap has the
    # opposite effect (converting from little-endian to big-endian), 
    # which is what we want. In both cases, the unpack('l') just 
    # produces a signed integer from those bytes, in the machine's 
    # native endianess.
    Array(num).pack('V').unpack('l')
end
可能不是最干净的,但这将转换字节数组。
def convertLEBytesToNative( bytes )
    if ( [1].pack('V').unpack('l').first == 1 )
        # machine is already little endian
        bytes.unpack('l')
    else
        # machine is big endian
        convertLEToNative( Array(bytes.unpack('l')))
    end
end
    
此问题有一种将签名转换为无符号的方法,可能会有所帮助。它还有一个指向bindata gem的指针,看起来它会做你想要的。
BinData::Int16le.read("00f") # 3072
[编辑删除不太正确的解包指令]     
为了后人,这里是我最终提出的方法,然后发现保罗鲁贝尔与“经典方法”的联系。它是kludgy并基于字符串操作,所以我可能会废弃它,但它确实有效,所以有一天有人可能会因为某些其他原因而感到有趣:
# Returns an integer from the given little-endian binary string.
# @param [String] str
# @return [Fixnum]
def self.bson_to_int(str)
  bits = str.reverse.unpack('B*').first   # Get the 0s and 1s
  if bits[0] == '0'   # We're a positive number; life is easy
    bits.to_i(2)
  else                # Get the twos complement
    comp, flip = "", false
    bits.reverse.each_char do |bit|
      comp << (flip ? bit.tr('10','01') : bit)
      flip = true if !flip && bit == '1'
    end
    ("-" + comp.reverse).to_i(2)
  end
end
更新:这是更简单的重构,使用Ken Bloom的广义任意长度形式的答案:
# Returns an integer from the given arbitrary length little-endian binary string.
# @param [String] str
# @return [Fixnum]
def self.bson_to_int(str)
  arr, bits, num = str.unpack('V*'), 0, 0
  arr.each do |int|
    num += int << bits
    bits += 32
  end
  num >= 2**(bits-1) ? num - 2**bits : num  # Convert from unsigned to signed
end
    

要回复问题请先登录注册