Java Utililty Methods IP Address Convert To

List of utility methods to do IP Address Convert To

Description

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

Method

StringconvertIp(String ip)
convert Ip
String[] iplist = ip.split("\\.");
int ip0 = Integer.parseInt(iplist[0]);
int ip1 = Integer.parseInt(iplist[1]);
int ip2 = Integer.parseInt(iplist[2]);
int ip3 = Integer.parseInt(iplist[3]);
String result = Integer.toHexString((ip0 << 24) + (ip1 << 16) + (ip2 << 8) + ip3);
for (int i = result.length(); i < 8; i++) {
    result = '0' + result;
...
StringconvertIP6Address(byte[] bytes)
convert IP Address
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 16; i = i + 2) {
    sb.append(Integer.toHexString(0xFF & bytes[i]));
    sb.append(Integer.toHexString(0xFF & bytes[i + 1]));
    if (i != 14) {
        sb.append(":");
return sb.toString();
byte[]convertIPAdressToBytes(String ip)
convert IP Adress 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;
longconvertIpPortToUniqueId(byte[] quad, int port)
convert Ip Port To Unique Id
return (((long) port) << 32 | ((long) (quad[0] & 0xFF)) << 24 | ((long) (quad[1] & 0xFF)) << 16
        | ((long) (quad[2] & 0xFF)) << 8 | ((long) (quad[3] & 0xFF)));
byte[]convertIpv4Address(String ip)
Convert an IPv4 address string representation into a sequence of 4 bytes.
String[] tokens = ip.split("\\.");
if (tokens.length != IPV4_BYTE_COUNT) {
    return null;
byte[] bytes = new byte[IPV4_BYTE_COUNT];
for (int index = 0; index < IPV4_BYTE_COUNT; index++) {
    try {
        int value = Integer.parseInt(tokens[index]);
...
StringconvertIpv4AddressToString(byte[] ipv4Address)
convert Ipv Address To String
if (ipv4Address.length != 4) {
    throw new IllegalArgumentException(
            "The input is too large, it must be of size 4. " + "The size is " + ipv4Address.length);
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < ipv4Address.length; i++) {
    int currentOctet = ipv4Address[i] & 0xFF;
    sb.append(currentOctet);
...
int[]convertIPv4NetworkPrefixLength(short length)
convert I Pv Network Prefix Length
switch (length) {
case 8:
    return new int[] { 255, 0, 0, 0 };
case 16:
    return new int[] { 255, 255, 0, 0 };
case 24:
    return new int[] { 255, 255, 255, 0 };
default:
...
StringconvertIpv4ToIpv6(String ip)
convert Ipv To Ipv
if (ip == null || ip.isEmpty()) {
    return ip;
String port = "";
if (ip.contains(":")) {
    port = ip.substring(ip.indexOf(":"));
    ip = ip.substring(0, ip.indexOf(":"));
String[] octets = ip.split("\\.");
if (octets.length == 4) {
    if (octets[0].contains("/")) {
        octets[0] = octets[0].substring(octets[0].indexOf("/"));
    final String s1 = Integer.toHexString(Integer.parseInt(octets[0]))
            + convertIntToHex(Integer.parseInt(octets[1]));
    final String s2 = Integer.toHexString(Integer.parseInt(octets[2]))
            + convertIntToHex(Integer.parseInt(octets[3]));
    return "[::ffff:" + (s1.startsWith("00") ? s1.substring(2) : s1) + ":"
            + (s2.startsWith("00") ? s2.substring(2) : s2) + "]" + port;
return ip;
byte[]convertIpv6Address(String ip)
Convert an IPv6 address string representation into a sequence of 16 bytes.
String temp = expandIpv6Address(ip);
String[] tokens = temp.split("\\:");
if (tokens.length != IPV6_SEGMENT_COUNT) {
    return null;
byte[] bytes = new byte[IPV6_BYTE_COUNT];
for (int index = 0, byteIndex = 0; index < IPV6_SEGMENT_COUNT; index++, byteIndex += 2) {
    try {
...
StringconvertIPV6Host(String host)
convert IPV Host
if (host.indexOf(':') != -1) {
    return ("[" + host + "]");
return (host);