Forum: Ruby inet_aton / inet_ntoa

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ba0976f0b91128a01af8c58d80b523ae?d=identicon&s=25 Franz Bettag (Guest)
on 2006-03-16 00:40
(Received via mailing list)
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!
--


Mit freundlichen Grüßen / best regards

Franz Bettag

____________________________
QS Housing
Franz Bettag
Ludwigstr. 45-47
90402 Nürnberg
http://www.qs-housing.net

Tel: +49 1805 737376 664
Fax: +49 1805 737376 665
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-16 01:41
(Received via mailing list)
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
3992dbf0a47456d2e1a1a1fe1511f75b?d=identicon&s=25 Claudio Jeker (Guest)
on 2006-03-16 09:28
(Received via mailing list)
On Thu, Mar 16, 2006 at 08:38:46AM +0900, 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?
>

require "ipaddr"

IPAddr.new("192.168.42.21").to_i
=> 3232246293
IPAddr.new(3232246293, Socket::AF_INET).to_s
=> "192.168.42.21"

Same works for Socket::AF_INET6.

Additionally you can do:

IPAddr.new_ntoh([ 3232246293 ].pack("N"))
=> #<IPAddr: IPv4:192.168.42.21/255.255.255.255>

IPAddr::new_ntoh creates a new ipaddr containing the given network byte
ordered string form of an IP address.
Ba0976f0b91128a01af8c58d80b523ae?d=identicon&s=25 Franz Bettag (Guest)
on 2006-03-16 10:14
(Received via mailing list)
Thanks to both of you! :)


Claudio Jeker schrieb:
>>
>
> Additionally you can do:
>
> IPAddr.new_ntoh([ 3232246293 ].pack("N"))
> => #<IPAddr: IPv4:192.168.42.21/255.255.255.255>
>
> IPAddr::new_ntoh creates a new ipaddr containing the given network byte
> ordered string form of an IP address.
>


--


Mit freundlichen
Grüßen
Franz Bettag

____________________________
QS Housing
Franz Bettag
Ludwigstr. 45-47
90402 Nürnberg
http://www.qs-housing.net

Tel: +49 1805 737376 664
Fax: +49 1805 737376 665
This topic is locked and can not be replied to.