Using ruby to convert unsigned integers stored as signed back to the original value

冷暖自知 提交于 2019-12-22 13:53:05

问题


A C-program is placing what it considers to be 64-bit unsigned integers into a column in a Postgres database that is typed as int8. To Postgres, int8 is always 'signed int8' (no such thing to it as 'unsigned int8'). So the Ruby program I have shows numbers retrieved from Postgres in the upper half of that space as negative.

What is the correct way, in Ruby, to take that -ve integer and convert it to the 64-bit unsigned integer that the C-programmer intended?


回答1:


I'm not sure of Ruby specifics, but essentially you need to add 2^64 if the number is negative. That's assuming the number is stored as 2's complement, which it almost certainly is.

FYI, the 2's complement system, essentially it treats a (say) 32-bit number as a number mod 2^32. This means -1 is the same thing as 2^32 - 1 or 0xFFFFFFFF. This turns out to be really simple to use at a hardware level.




回答2:


This might work:

x += 0x1_0000_0000_0000_0000 if x < 0

The huge constant is 2 to the 64th, as a normal literal. I added the underscores to make it easier to verify that the number is indeed a one followed by 64 bits of zeroes. Each hex digit represents four bits, as always. The underscores are standard Ruby syntax.



来源:https://stackoverflow.com/questions/301259/using-ruby-to-convert-unsigned-integers-stored-as-signed-back-to-the-original-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!