求ruby 將ip長度換算成掩碼? 比如:41.58.0.0 - 41.58.255.255 65536 將65536 分別換算成255.255.0.0/16
不會ruby ,請幫助謝謝!
认证高级PHP讲师
def mask(ip1, ip2) num = ->(ip) { n=ip.split("."); n[0].to_i()*0x1000000+n[1].to_i()*0x10000+n[2].to_i()*0x100+n[3].to_i() } m=(/(1+)$/.match((num.call(ip1)^num.call(ip2)).to_s(2))[1]) nip = m.to_i(2)^0xFFFFFFFF (nip/0x1000000%0x100).to_s + '.' + (nip/0x10000%0x100).to_s + '.' + (nip/0x100%0x100).to_s + '.' + (nip%0x100).to_s + '/' + m.size.to_s end
測驗:
irb(main):001:0> mask("41.58.0.0", "41.58.255.255") => "255.255.0.0/16"
測驗: