On Thu, 16 Mar 2006, Franz Bettag wrote:

> Hi guys,
>
> i have a big problem with ruby + inet_aton.  Since IPAddr has the to_i
> function it can convert IPAddrs to integers (inet_aton). But i didn't find
> any function that reverses this (inet_ntoa)..  And without such a function,
> i can't work =)
>
> Any ideas?
>
> thanks!

maybe this will work


     harp:~ > cat a.rb
     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


     ip = "255.127.63.31"
     n = inet_aton ip

     puts n
     puts n.to_s(2)

     a = inet_ntoa n
     puts a

     puts(inet_ntoa(inet_aton(ip)))


     harp:~ > ruby a.rb
     4286529311
     11111111011111110011111100011111
     255.127.63.31
     255.127.63.31

i can't remember if that's exactly how those work - but i thinks that's right.

hth.

-a
-- 
share your knowledge.  it's a way to achieve immortality.
- h.h. the 14th dali lama