<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ip address code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 06:13:11 GMT</pubDate>
    <description>DZone Snippets: ip address code</description>
    <item>
      <title>Format IPv4 address in octal binary format and vice versa (Ruby / Rails)</title>
      <link>http://snippets.dzone.com/posts/show/4931</link>
      <description>Format an IPv4 address like 192.168.1.1 in dotted binary format like 11000000.10101000.00000001.00000001&lt;br /&gt;You also need this class: http://snippets.dzone.com/posts/show/2472&lt;br /&gt;For Rails: Put this in your controller!&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # convert a dotted decimal IPv4 address to dotted binary format&lt;br /&gt;  def ipv4_to_binary(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_i.to_s(2).using("########","0",true)&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  &lt;br /&gt;  # convert a IPv4 adress in binary dotted format to a dotted IPv4 address&lt;br /&gt;  def binary_to_ipv4(ipv4addr)&lt;br /&gt;    ia = ipv4addr.to_s.split('.')&lt;br /&gt;    if ia.size != 4&lt;br /&gt;      return "0.0.0.0"&lt;br /&gt;    end&lt;br /&gt;    output = ""&lt;br /&gt;    i = 1&lt;br /&gt;    for octett in ia&lt;br /&gt;      output = output + octett.to_s.to_i(2).to_s&lt;br /&gt;      if i &lt; 4&lt;br /&gt;        output = output + "."&lt;br /&gt;      end&lt;br /&gt;      i += 1&lt;br /&gt;    end&lt;br /&gt;    return output&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Dec 2007 14:12:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4931</guid>
      <author>MichaelWhi (Michael Whittaker)</author>
    </item>
  </channel>
</rss>
