Java Utililty Methods Long to IP Address

List of utility methods to do Long to IP Address

Description

The list of methods to do Long to IP Address are organized into topic(s).

Method

int[]longToIp(long address)
A convenient method that accepts an IP address represented as a long and returns an integer array of size 4 representing the same IP address.
int[] ip = new int[4];
for (int i = 3; i >= 0; i--) {
    ip[i] = (int) (address % 256);
    address = address / 256;
return ip;
StringlongToIp(long i)
long To Ip
return ((i >> 24) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + (i & 0xFF);
StringlongToIp(long i)
long To Ip
long ipOnly = i & 0xFFFFFFFF;
long netmask = (long) (i >> NUMBER_OF_BITS_IN_IP);
StringBuffer result = new StringBuffer();
result.append((ipOnly >> TWENTY_FOUR) & X_FF);
result.append(DOT);
result.append((ipOnly >> SIXTEEN) & X_FF);
result.append(DOT);
result.append((ipOnly >> EIGHT) & X_FF);
...
StringlongToIp(long ip)
covert the ip address from decimal to dot formatted
StringBuilder sb = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
    sb.insert(0, Long.toString(ip & 0xff));
    if (i < 3) {
        sb.insert(0, '.');
    ip = ip >> 8;
return sb.toString();
StringlongToIp(long ip)
long To Ip
StringBuilder sb = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
    sb.insert(0, Long.toString(ip & 0xff));
    if (i < 3) {
        sb.insert(0, '.');
    ip >>= 8;
return sb.toString();
StringLongToIp(long longIp)
Long To Ip
StringBuffer sb = new StringBuffer("");
sb.append(String.valueOf((longIp >>> 24)));
sb.append(".");
sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));
sb.append(".");
sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));
sb.append(".");
sb.append(String.valueOf((longIp & 0x000000FF)));
...
StringlongToIp(long longValue)
long To Ip
int[] byteIPAddress = { 0, 0, 0, 0 };
for (int i = 3; i >= 0; i--) {
    byteIPAddress[i] = (int) (longValue % 256);
    longValue = longValue >> 8;
String returnValue = new String(
        byteIPAddress[0] + "." + byteIPAddress[1] + "." + byteIPAddress[2] + "." + byteIPAddress[3]);
return returnValue;
...
StringlongToIp(String ip)
Converts a String IP address to a long String representation.
Long ipLong = Long.valueOf(ip);
StringBuilder result = new StringBuilder(15);
for (int i = 0; i < 4; i++) {
    result.insert(0, Long.toString(ipLong & 0xff));
    if (i < 3) {
        result.insert(0, '.');
    ipLong = ipLong >> 8;
...
StringlongToIpAddress(long ip)
Returns the 32bit dotted format of the provided long ip.
if (ip > 4294967295l || ip < 0) {
    throw new IllegalArgumentException("invalid ip");
StringBuilder ipAddress = new StringBuilder();
for (int i = 3; i >= 0; i--) {
    int shift = i * 8;
    ipAddress.append((ip & (0xff << shift)) >> shift);
    if (i > 0) {
...
StringlongToIPv4(final long ipaddress)
Convert a long to an IP Address, as four dotted octets.
final String SOURCE = "NetworkScannerHelper.longToIPv4() ";
final int octet1;
final int octet2;
final int octet3;
final int octet4;
final String strIP;
octet1 = (int) ((ipaddress & 0x00000000FF000000L) >>> 24);
octet2 = (int) ((ipaddress & 0x0000000000FF0000L) >>> 16);
...