module Aio::Base::Toolkit::IPAddr

Public Class Methods

mask_dotted_to_i(mask) click to toggle source

子网掩码从点分十进制转换为数值 255.255.255.0 => 24

# File lib/aio/base/toolkit/ipaddr.rb, line 6
def mask_dotted_to_i(mask)
        arr = mask.split(".")
        binary = ""
        arr.each do |m|
                binary << m.to_i.to_s(2)
        end
        return binary.scan(/1/).size
end
mask_i_to_dotted(int) click to toggle source

子网掩码从十进制到点分十进制 24 => 255.255.255.0

# File lib/aio/base/toolkit/ipaddr.rb, line 17
def mask_i_to_dotted(int)
        int = int.to_i
        arr = [nil, nil, nil, nil]
        binary = (2 ** int - 1) << (32 - int)
        4.times do |t|
                arr[3-t] = (binary & 255).to_s
                binary = binary >> 8
        end
        return arr.join(".")
end