Skip to content

Instantly share code, notes, and snippets.

@sitz
Last active December 19, 2015 21:39

Revisions

  1. sitz revised this gist Jul 17, 2013. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions IPv4Converter.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package com.skenzo.test;

    /**
    * Created with IntelliJ IDEA.
    * User: sitesh
  2. sitz created this gist Jul 17, 2013.
    33 changes: 33 additions & 0 deletions IPv4Converter.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    package com.skenzo.test;

    /**
    * Created with IntelliJ IDEA.
    * User: sitesh
    * Date: 7/17/13
    * Time: 9:24 PM
    * To change this template use File | Settings | File Templates.
    */
    public class IPv4Converter {

    private static String longToIp(int ip) {
    int[] ipParts = new int[4];

    ipParts[0] = 0xFF & (ip >> 24);
    ipParts[1] = 0xFF & (ip >> 16);
    ipParts[2] = 0xFF & (ip >> 8);
    ipParts[3] = 0xFF & (ip >> 0);

    return ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + "." + ipParts[3];
    }

    private static int ipToLong(String ipAddress) {
    String[] ipParts = ipAddress.split("\\.");

    int part0 = Integer.parseInt(ipParts[0]) << 24;
    int part1 = Integer.parseInt(ipParts[1]) << 16;
    int part2 = Integer.parseInt(ipParts[2]) << 8;
    int part3 = Integer.parseInt(ipParts[3]) << 0;

    return part0 + part1 + part2 + part3;
    }
    }