我有一个从C程序收到的编号作为负数来到我这里:
-1771632774
它应该是这个数字:
2523334522
我意识到这必须是由于从有符号整数到无符号整数的转换 . 现在我在Ruby中有这个负数,我怎样才能将它转换回无符号版本?
将负整数放在数组中 . 使用 'L' 的参数调用 pack ,表示"32-bit unsigned, native endian (uint32_t)" . 使用相同的参数调用unpack . 最后,从数组中获取数字 .
'L'
pack
[-1771632774].pack('L').unpack('L').first #=> 2523334522
http://ruby-doc.org/core-2.4.0/Array.html#method-i-pack
1 回答
将负整数放在数组中 . 使用
'L'
的参数调用pack
,表示"32-bit unsigned, native endian (uint32_t)" . 使用相同的参数调用unpack . 最后,从数组中获取数字 .http://ruby-doc.org/core-2.4.0/Array.html#method-i-pack