Java IP Address to Long ipToLong(String ipStr)

Here you can find the source of ipToLong(String ipStr)

Description

Ip converted to type long.

License

Open Source License

Parameter

Parameter Description
ipStr string of ip

Return

ipLongArr[]

Declaration

public static long[] ipToLong(String ipStr) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  ww w  . j  a  va 2 s . c  o  m*/
     * Ip converted to type long.
     * 
     * @param ipStr
     *            string of ip
     * @return ipLongArr[]
     */
    public static long[] ipToLong(String ipStr) {
        long[] ipLongArr = new long[2];
        final int leftShift3 = 24;
        final int leftShift2 = 16;
        final int leftShift1 = 8;
        if (ipStr.indexOf(".") >= 0) {
            String[] ip = ipStr.split("\\.");
            ipLongArr[1] = (Long.parseLong(ip[0]) << leftShift3) + (Long.parseLong(ip[1]) << leftShift2)
                    + (Long.parseLong(ip[2]) << leftShift1) + Long.parseLong(ip[3]);
            ipLongArr[0] = 0;
        } else {
            return ipV6ToLong(ipStr);
        }
        return ipLongArr;
    }

    /**
     * string of IpV6 converted to long.
     * 
     * @param longip
     *            a ipv6 string
     * @return long[]
     */
    public static long[] ipV6ToLong(String longip) {
        String[] strings = expandShortNotation(longip).split(":");
        long[] longs = new long[strings.length];

        long high = 0L;
        long low = 0L;
        final int radix = 16;
        final int fieldSize = 4;
        for (int i = 0; i < strings.length; i++) {
            if (i >= 0 && i < fieldSize) {
                high |= (Long.parseLong(strings[i], radix) << ((longs.length - i - 1) * radix));
            } else {
                low |= (Long.parseLong(strings[i], radix) << ((longs.length - i - 1) * radix));
            }
        }
        longs[0] = high;
        if (longs.length > 1) {
            longs[1] = low;
        }
        return longs;
    }

    /**
     * The abbreviated IPv6 converted into a standard wording.
     * 
     * @param strIp
     *            ip string
     * @return ipv6String
     */
    public static String expandShortNotation(String strIp) {
        final String strDoubleColon = "::";
        final String strSingleColon = ":";
        final int allIpV6Colons = 7;
        int allColons = allIpV6Colons;
        if (!strIp.contains(strDoubleColon)) {
            return strIp;
        } else if (strIp.equals(strDoubleColon)) {
            return generateZeroes(allColons + 1);
        } else {
            final int numberOfColons = countOccurrences(strIp, ':');

            if (strIp.indexOf(".") >= 0) {
                allColons -= 1;
            }
            if (strIp.startsWith(strDoubleColon)) {
                return strIp.replace(strDoubleColon, generateZeroes((allColons + 2) - numberOfColons));
            } else if (strIp.endsWith(strDoubleColon)) {
                return strIp.replace(strDoubleColon,
                        strSingleColon + generateZeroes((allColons + 2) - numberOfColons));
            } else {
                return strIp.replace(strDoubleColon,
                        strSingleColon + generateZeroes((allColons + 2 - 1) - numberOfColons));
            }
        }
    }

    /**
     * Generated IPv6 address 0.
     * 
     * @param number
     *            from number to string
     * @return ipv6String
     */
    public static String generateZeroes(int number) {
        final StringBuilder builder = new StringBuilder();
        for (int i = 0; i < number; i++) {
            builder.append("0:");
        }

        return builder.toString();
    }

    /**
     * The record ipv6 address: Number of.
     * 
     * @param haystack
     *            the string
     * @param needle
     *            the split char
     * @return count
     */
    public static int countOccurrences(String haystack, char needle) {
        int count = 0;
        for (int i = 0; i < haystack.length(); i++) {
            if (haystack.charAt(i) == needle) {
                count++;
            }
        }
        return count;
    }
}

Related

  1. ipToLong(String ip)
  2. ipToLong(String ip)
  3. ipToLong(String ip)
  4. ipToLong(String ipAddress)
  5. ipToLong(String ipAddress)
  6. ipToLong(String sip)
  7. ipToLong(String strIP)
  8. ipv4(long ip)
  9. ipV4AddressToLong(String ipAddress)