Java IP Address to String ipv4ToStr(int ipv4)

Here you can find the source of ipv4ToStr(int ipv4)

Description

Converts an IPv4 address from integer representation to dotted decimal notation (e.g., "192.168.1.1").

License

Open Source License

Parameter

Parameter Description
ipv4 the IPv4 address to be converted (must be in big endian byte-order)

Return

dotted decimal notation

Declaration

static public String ipv4ToStr(int ipv4) 

Method Source Code

//package com.java2s;
/**//  ww w.java2  s .c o  m
 * NetUtil
 * Copyright (c) 2014 Frank Duerr
 *
 * NetUtil is part of SDN-MQ. 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
 */

public class Main {
    /**
     * Converts an IPv4 address from integer representation to dotted decimal notation (e.g., "192.168.1.1").
     * @param ipv4 the IPv4 address to be converted (must be in big endian byte-order)
     * @return dotted decimal notation
     */
    static public String ipv4ToStr(int ipv4) {
        short[] decimals = new short[4];

        for (int i = 0; i < 4; i++) {
            decimals[i] = (short) (ipv4 & 0xff);
            ipv4 >>= 8;
        }

        String dottedStr = Short.toString(decimals[3]) + "." + Short.toString(decimals[2]) + "."
                + Short.toString(decimals[1]) + "." + Short.toString(decimals[0]);

        return dottedStr;
    }
}

Related

  1. IpToString(int address)
  2. IPToString(int intIP)
  3. ipToString(long ip)
  4. ipv4ToBinaryStr(int ipv4)
  5. ipV4ToLong(final String ipaddress)
  6. ipv4ToString(byte[] address)
  7. ipV4ToString(int ip)