Calculates the network mask address from the value - Android Network

Android examples for Network:Network Operation

Description

Calculates the network mask address from the value

Demo Code


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

public class Main {
    /**//from  w ww  .  j a v  a 2s. c  o  m
     * Calculates the network mask address from the value
     * 
     * @param numericMask
     *            the network mask
     * @return
     */
    public static String networkMaskFromInt(int numericMask) {

        int value = 0xffffffff << (32 - numericMask);
        byte[] bytes = new byte[] { (byte) (value >>> 24),
                (byte) (value >> 16 & 0xff), (byte) (value >> 8 & 0xff),
                (byte) (value & 0xff) };

        InetAddress netAddr = null;
        try {
            netAddr = InetAddress.getByAddress(bytes);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return netAddr.getHostAddress();
    }
}

Related Tutorials