Java IP Address Convert To convertIpv4AddressToString(byte[] ipv4Address)

Here you can find the source of convertIpv4AddressToString(byte[] ipv4Address)

Description

convert Ipv Address To String

License

Open Source License

Declaration

public static String convertIpv4AddressToString(byte[] ipv4Address) 

Method Source Code

//package com.java2s;
/*//ww  w. j  a  va 2 s  . co  m
 * lib-bitcoinj
 * Copyright (c) 2014, Stephen Liang, All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.
 */

public class Main {
    public static String convertIpv4AddressToString(byte[] ipv4Address) {
        if (ipv4Address.length != 4) {
            throw new IllegalArgumentException(
                    "The input is too large, it must be of size 4. "
                            + "The size is " + ipv4Address.length);
        }

        StringBuilder sb = new StringBuilder("");

        for (int i = 0; i < ipv4Address.length; i++) {
            int currentOctet = ipv4Address[i] & 0xFF;

            sb.append(currentOctet);

            if (i != 3) {
                sb.append(".");
            }
        }

        return sb.toString();
    }
}

Related

  1. convertIp(String ip)
  2. convertIP6Address(byte[] bytes)
  3. convertIPAdressToBytes(String ip)
  4. convertIpPortToUniqueId(byte[] quad, int port)
  5. convertIpv4Address(String ip)
  6. convertIPv4NetworkPrefixLength(short length)
  7. convertIpv4ToIpv6(String ip)
  8. convertIpv6Address(String ip)
  9. convertIPV6Host(String host)