Java Long to IP Address longToIpV6(long highBits, long lowBits)

Here you can find the source of longToIpV6(long highBits, long lowBits)

Description

convert ipV6 Long format to String.

License

Open Source License

Parameter

Parameter Description
highBits high 64 bits long value.
lowBits low 64 bits long value.

Return

ipv6 string.

Declaration

public static String longToIpV6(long highBits, long lowBits) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w w  w .  j a  va  2 s  .c  o m
     * convert ipV6 Long format to String.
     * 
     * @param highBits
     *            high 64 bits long value.
     * @param lowBits
     *            low 64 bits long value.
     * @return ipv6 string.
     */
    public static String longToIpV6(long highBits, long lowBits) {
        final int oneByteSize = 8;
        final int v6MaxSegment = 8;
        final int twoByteSize = 16;
        final int fourByteMask = 0xFFFF;
        short[] shorts = new short[v6MaxSegment];
        String[] strings = new String[shorts.length];
        for (int i = 0; i < v6MaxSegment; i++) {
            if (i >= 0 && i < v6MaxSegment / 2) {
                strings[i] = String.format("%x",
                        (short) (((highBits << i * twoByteSize) >>> twoByteSize * (oneByteSize - 1))
                                & fourByteMask));
            } else {
                strings[i] = String.format("%x",
                        (short) (((lowBits << i * twoByteSize) >>> twoByteSize * (oneByteSize - 1))
                                & fourByteMask));
            }
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            result.append(strings[i]);
            if (i < strings.length - 1) {
                result.append(":");
            }
        }
        return result.toString();
    }
}

Related

  1. longToIpAddress(long ip)
  2. longToIPv4(final long ipaddress)
  3. longToIpV4(long ip)
  4. longToIpV4(long longIp)
  5. longToIpV4(long longIp)