Android IP Address Get getIPV4NetwprkOrder(String theIp)

Here you can find the source of getIPV4NetwprkOrder(String theIp)

Description

turn ip in string representation to byte array in network order.

Parameter

Parameter Description
theIp a parameter

Exception

Parameter Description
UnknownHostException an exception

Return

ip as byte array

Declaration

public static byte[] getIPV4NetwprkOrder(String theIp)
        throws UnknownHostException 

Method Source Code

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

public class Main {
    private static final int IPV4_ADDRESS_LEN = 4;

    /**/*from   w ww .j ava2s  .  c  o  m*/
     * turn ip in string representation to byte array in network order.
     * @param theIp
     * @return ip as byte array
     * @throws UnknownHostException
     */
    public static byte[] getIPV4NetwprkOrder(String theIp)
            throws UnknownHostException {
        byte[] toReturn = new byte[IPV4_ADDRESS_LEN];

        String[] fields = theIp.split("\\.");

        if (fields == null || fields.length < IPV4_ADDRESS_LEN)
            throw new UnknownHostException();

        for (int i = 0; i < fields.length; i++) {
            toReturn[i] = (byte) Integer.parseInt(fields[i]);
        }
        return toReturn;
    }
}

Related

  1. getTargetInetaddress(String target)
  2. getDevicesMac(Context context)
  3. getDevicesIP(Context context)
  4. getIPs()
  5. getIPv4StringByStrippingIPv6Prefix(String in)