Java 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.

License

Apache License

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;
/*/*from  w w  w  . ja va  2  s  . c  o m*/
 *  Copyright 2017 AT&T
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

import java.net.UnknownHostException;

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

    /**
     * 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. getIPv4Address()
  2. getIPV4Address(List addresses)
  3. getIPv4Address(NetworkInterface iface)
  4. getIPV4MainAddress(NetworkInterface ni)
  5. getIPv4MulticastGroup(int hash)
  6. getIPV6AddrArray(String s)
  7. getIPV6AllAddresses(NetworkInterface ni)
  8. getIpv6List()
  9. getLanIPAddress()