Class: Etheruby::Encoders::Int

Inherits:
Base
  • Object
show all
Defined in:
lib/etheruby/encoders/int.rb

Instance Attribute Summary

Attributes inherited from Base

#data

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Etheruby::Encoders::Base

Instance Method Details

#decodeObject



17
18
19
20
21
22
23
24
25
# File 'lib/etheruby/encoders/int.rb', line 17

def decode
  in_int = data[0..63].to_i(16)
  in_bin = in_int.to_s(2).rjust(64, '0')
  if in_bin[0] == '1'
    return -(in_bin.split("").map{ |i| i == '1' ? '0' : '1' }.join.to_i(2) + 1), 32
  else
    return in_int, 32
  end
end

#encode(pad_to = 64) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/etheruby/encoders/int.rb', line 7

def encode(pad_to=64)
  @data = data.to_i if data.is_a? ::String
  if data >= 0
    data.to_s(16).rjust(pad_to, '0')
  else
    mask = (1 << pad_to*4) - 1
    (data & mask).to_s(16).rjust(pad_to, 'f')
  end
end