Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/wwmd/class_extensions/extensions_base.rb

Instance Method Summary collapse

Instance Method Details

#int_to_ipObject

integer to ip address



25
26
27
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 25

def int_to_ip
  [24, 16, 8, 0].map { |b| (self >> b) & 255 }.join('.')
end

#int_to_macObject

integer to mac address [uses ‘:’ as delimiter]



30
31
32
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 30

def int_to_mac
  [40,32,24,16,8,0].map { |b| ((self >> b) & 255).to_s(16).rjust(2,"0") }.join(":")
end

#to_bin(len, rev = false) ⇒ Object

return binary representation of length size padded with x00

length:  length in bytes to return (padded with least signficant \x00
reverse: reverse the byte order


13
14
15
16
17
18
19
20
21
22
# File 'lib/wwmd/class_extensions/extensions_base.rb', line 13

def to_bin (len,rev = false)
  str = ""
  bignum = self
  1.upto(len) do |i|
    str << (bignum & 0xFF).to_n8
    bignum = bignum >> 8  
  end
  return str.reverse if rev
  return str
end