Here you can find the source of longToIpV6(long highBits, long lowBits)
Parameter | Description |
---|---|
highBits | high 64 bits long value. |
lowBits | low 64 bits long value. |
public static String longToIpV6(long highBits, long lowBits)
//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(); } }