transform IP address int value to InetAddress - Android Network

Android examples for Network:IP Address

Description

transform IP address int value to InetAddress

Demo Code


//package com.java2s;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static InetAddress transform(int ip) throws UnknownHostException {
        byte[] arrayOfByte = new byte[4];
        arrayOfByte[3] = ((byte) (0xFF & (ip >> 24)));
        arrayOfByte[2] = ((byte) (0xFF & (ip >> 16)));
        arrayOfByte[1] = ((byte) (0xFF & (ip >> 8)));
        arrayOfByte[0] = ((byte) (ip & 0xFF));
        InetAddress localInetAddress = InetAddress
                .getByAddress(arrayOfByte);
        return localInetAddress;
    }//from  w  w w  .j  av  a  2s.co  m
}

Related Tutorials