Skip to content

Instantly share code, notes, and snippets.

@kron4eg
Created March 18, 2009 18:21
Show Gist options
  • Save kron4eg/81299 to your computer and use it in GitHub Desktop.
Save kron4eg/81299 to your computer and use it in GitHub Desktop.
module IntIp
def ip2int(ip)
return 0 unless ip =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/
v = ip.split('.').collect { |i| i.to_i }
return (v[0] << 24) | (v[1] << 16) | (v[2] << 8 ) | (v[3]);
end
def int2ip(int)
tmp = int.to_i
parts = []
3.times do |i|
tmp = tmp / 256.0
parts << (256 * (tmp - tmp.to_i)).to_i
end
parts << tmp.to_i
parts.reverse.join('.')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment