Skip to content

Instantly share code, notes, and snippets.

@samullen
Created October 11, 2010 20:44
Show Gist options
  • Select an option

  • Save samullen/621189 to your computer and use it in GitHub Desktop.

Select an option

Save samullen/621189 to your computer and use it in GitHub Desktop.
IP to Integer and back
# IP to Integer
ip = IPAddr.new('255.255.255.255')
puts ip.to_i
# Integer to IP
ipnum = 4294967295
ip = IPAddr.new(ipnum, Socket::AF_INET).to_s
# If you store the ipnum in MySQL, make sure to use unsigned into for the field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment