title: Conversion IPv6 to long and long to IPv6
body: <p>How should I perform conversion from IPv6 to long and vice versa?</p>
  
  <p>So far I have:</p>
  
  <pre class="lang-java prettyprint-override"><code>    public static long IPToLong(String addr) {
              String[] addrArray = addr.split("\\.");
              long num = 0;
              for (int i = 0; i &lt; addrArray.length; i++) {
                      int power = 3 - i;
  
                      num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
              }
              return num;
      }
  
      public static String longToIP(long ip) {
              return ((ip &gt;&gt; 24) &amp; 0xFF) + "."
                      + ((ip &gt;&gt; 16) &amp; 0xFF) + "."
                      + ((ip &gt;&gt; 8) &amp; 0xFF) + "."
                      + (ip &amp; 0xFF);
  
      }
  </code></pre>
  
  <p>Is it correct solution or I missed something?</p>
  
  <p>(It would be perfect if the solution worked for both ipv4 and ipv6)</p>