Java IP Address Convert To convertIP6Address(byte[] bytes)

Here you can find the source of convertIP6Address(byte[] bytes)

Description

convert IP Address

License

Open Source License

Declaration

public static String convertIP6Address(byte[] bytes) 

Method Source Code

//package com.java2s;
/**/*from ww  w  . j  ava2s . co  m*/
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */

public class Main {
    public static byte[] convertIP6Address(String fullFormIP6Address) {
        byte[] retVal = new byte[16];
        String[] ip6Split = fullFormIP6Address.split(":");
        for (int i = 0; i < 8; i++) {
            ;
            String octet = ip6Split[i];
            StringBuffer sb = new StringBuffer();

            while ((sb.length() + octet.length()) < 4) {
                sb.append("0");
            }
            sb.append(octet);

            retVal[i * 2] = (byte) Short.parseShort(sb.toString().substring(0, 2), 16);
            retVal[i * 2 + 1] = (byte) Short.parseShort(sb.toString().substring(2, 4), 16);
        }

        return retVal;
    }

    public static String convertIP6Address(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 16; i = i + 2) {
            sb.append(Integer.toHexString(0xFF & bytes[i]));
            sb.append(Integer.toHexString(0xFF & bytes[i + 1]));
            if (i != 14) {
                sb.append(":");
            }
        }
        return sb.toString();
    }
}

Related

  1. convertIp(String ip)
  2. convertIPAdressToBytes(String ip)
  3. convertIpPortToUniqueId(byte[] quad, int port)
  4. convertIpv4Address(String ip)
  5. convertIpv4AddressToString(byte[] ipv4Address)