Netmask to CIDR in ruby

前端 未结 7 921
灰色年华
灰色年华 2020-12-29 11:13

I\'ve been using the ip-address gem and it doesn\'t seem to have the ability to convert from a netmask of the form

255.255.255.0 

into the

7条回答
  •  天命终不由人
    2020-12-29 11:34

    Here is a way to do it without the IPAddr gem

    (('1'*cidr)+('0'*(32-cidr))).scan(/.{8}/m).map{|e|e.to_i(2)}.join('.')
    

提交回复
热议问题