Class: Lwes::Serialization::IpV4

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/lwes/serialization.rb

Instance Method Summary collapse

Instance Method Details

#getObject



98
99
100
# File 'lib/lwes/serialization.rb', line 98

def get
  self.int32_to_ip(self.data)
end

#int32_to_ip(num) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/lwes/serialization.rb', line 87

def int32_to_ip(num)
  ary = []
  
  4.times do
    ary << (num & 0xff)
    num = num >> 8
  end

  ary.join(".")
end

#ip_to_int32(str) ⇒ Object



82
83
84
85
# File 'lib/lwes/serialization.rb', line 82

def ip_to_int32(str)
  ary = str.to_s.split(".").collect{ |byte| byte.to_i }.slice(0,4)
  ((ary[3] & 0xff) << 24) + ((ary[2] & 0xff) << 16) + ((ary[1] & 0xff) << 8) + (ary[0] & 0xff)
end

#set(val) ⇒ Object



102
103
104
# File 'lib/lwes/serialization.rb', line 102

def set(val)
  self.data = ip_to_int32(val)
end