Java Utililty Methods IP Address to Byte Array

List of utility methods to do IP Address to Byte Array

Description

The list of methods to do IP Address to Byte Array are organized into topic(s).

Method

byte[]ip2bytes(String ip)
ipbytes
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");
...
byte[]ip2bytes(String ipAddr)
ipbytes
byte[] ret = new byte[4];
try {
    String[] ipArr = ipAddr.split("\\.");
    ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);
    return ret;
...
byte[]ipToByte(String ip)
ip To Byte
String[] spl1 = ip.split("\\.");
byte[] spl2 = new byte[spl1.length];
for (int i = 0; i < spl1.length; i++) {
    spl2[i] = (byte) Integer.parseInt(spl1[i]);
return spl2;
int[]ipToByte(String ip)
ip To Byte
int[] result = new int[4];
String[] ipPart = ip.split("\\.");
result[0] = Integer.parseInt(ipPart[0]);
result[1] = Integer.parseInt(ipPart[1]);
result[2] = Integer.parseInt(ipPart[2]);
result[3] = Integer.parseInt(ipPart[3]);
return result;
byte[]IpToBytes(String ip)
Ip To Bytes
String[] ipStr = ip.split("\\.");
byte[] bytes = new byte[ipStr.length];
for (int i = 0; i < ipStr.length; i++) {
    bytes[i] = Integer.valueOf(ipStr[i], 10).byteValue();
return bytes;
byte[]ipToBytesByReg(String ipAddr)
ip To Bytes By Reg
byte[] ret = new byte[4];
try {
    String[] ipArr = ipAddr.split("\\.");
    ret[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ret[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ret[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ret[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);
    return ret;
...
byte[]ipv4StringToByteArrayUnchecked(String str)
ipv String To Byte Array Unchecked
byte[] ret = new byte[4];
String[] s = str.split("\\.");
for (int i = 0; i < ret.length; i++) {
    ret[i] = (byte) Integer.parseInt(s[i], 10);
return ret;
byte[]ipv4ToBytes(String ipv4)
ipv To Bytes
String[] groups = ipv4.split("\\.");
byte[] ret = new byte[4];
for (int i = 0; i < 4; i++) {
    ret[i] = (byte) Integer.parseInt(groups[i]);
return ret;
byte[]ipv4ToBytes(String ipv4)
IPv4 String to bytes.
if (ipv4 == null || ipv4.isEmpty()) {
    return null;
final String[] segments = ipv4.split("\\.", -1);
if (segments.length != 4) {
    return null;
final byte[] ret = new byte[4];
...
byte[]ipv4ToBinary(String ip)
ipv To Binary
try {
    return InetAddress.getByName(ip).getAddress();
} catch (Exception e) {
    return new byte[4];