inet_ntoa and inet_aton in Ruby

Posted by Dan Sosedoff on April 16, 2009

Here is two small functions to convert IP addresses from string representation to integer and vice versa.

def inet_aton(ip)
    ip.split(/\./).map{|c| c.to_i}.pack("C*").unpack("N").first
end
 
def inet_ntoa(n)
    [n].pack("N").unpack("C*").join "."
end
Trackbacks

Trackbacks are closed.

Comments

Comments are closed.