Class: DataInspect::UnsignedToSignedInteger

Inherits:
Object
  • Object
show all
Defined in:
lib/data_inspect.rb

Class Method Summary collapse

Class Method Details

.toSignedInteger(unsigned_value, size) ⇒ Object

Size is in bytes, and should be 1, 2 or 4.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/data_inspect.rb', line 121

def self.toSignedInteger(unsigned_value, size)
  subtrahend = 2 ** (size * 8)

  puts "subtrahend is #{subtrahend}."

  max_value = 2 ** (size * 8 - 1) - 1
  puts "max value is #{max_value}"

  result = unsigned_value
  result -= subtrahend if (unsigned_value > max_value)

  return result
end