interger to I P address

Aaron Gifford astounding at gmail.com
Wed Aug 27 17:05:32 CDT 2008


Ruby's IPAddr class is quite handy for IPv4 and IPv6 integer
representation conversions.

For IP to integer, whether IPv4 or IPv6, ruby code:

  require 'ipaddr'
  print "#{IPAddr.new('10.0.0.55').to_i}\n"
  print "#{IPAddr.new('2001:0db8:85a3:08d3:1319:8a2e:0370:7334').to_i}\n"

Results in:
  167772215
  42540766452641195744311209248773141300

And for integer to IPv4, ruby code:

  require "ipaddr"
  print "#{IPAddr.new(167772215,Socket::AF_INET)}\n"

Results in:
  10.0.0.55

And for integer to IPv6, ruby code:

  require 'ipaddr'
  print "#{IPAddr.new(42540766452641195744311209248773141300,
Socket::AF_INET6)}\n"

Results in:
  2001:0db8:85a3:08d3:1319:8a2e:0370:7334

Aaron out.




More information about the NANOG mailing list