Skip to content

Instantly share code, notes, and snippets.

@vvalgis
Last active October 12, 2015 02:38
Show Gist options
  • Save vvalgis/3958687 to your computer and use it in GitHub Desktop.
Save vvalgis/3958687 to your computer and use it in GitHub Desktop.
convert ip to integer
# Better version from
# http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/
[24, 16, 8, 0].map{|b| (1089052672 >> b) & 255}.join('.')
"64.233.160.0".split('.').zip([256**3, 256**2, 256, 1]).map{|o| o[0].to_i * o[1]}.inject(0){|s,o| s += o}
# Better version from
# http://basic70tech.wordpress.com/2007/04/13/32-bit-ip-address-to-dotted-notation-in-ruby/
"64.233.160.0".split('.').inject(0) {|s,o| (s << 8 ) + o.to_i}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment