Java IP Address to Byte Array ip2bytes(String ip)

Here you can find the source of ip2bytes(String ip)

Description

ipbytes

License

Apache License

Declaration

static byte[] ip2bytes(String ip) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    static byte[] ip2bytes(String ip) {
        String[] addrs = ip.split("\\.");
        if (addrs.length != 4)
            throw new IllegalArgumentException("ip should be a x.x.x.x");
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++) {
            int a = Integer.parseInt(addrs[i]);
            if (a > 255)
                throw new IllegalArgumentException("not a legal ip address");
            bytes[i] = (byte) a;
        }/*  w w  w  . j  av a2  s . co m*/
        return bytes;
    }
}

Related

  1. ip2bytes(String ipAddr)
  2. ipToByte(String ip)
  3. ipToByte(String ip)
  4. IpToBytes(String ip)