Different representation of IPV4 in Java

🌐 IPv4 Address — Different Representations & Normalization Using Java

When working with networks, operating systems, or low-level protocols, an IPv4 address may not always appear in the familiar dotted-decimal form like:

192.0.2.235

An IPv4 address is fundamentally a 32-bit integer, and therefore it can be represented in multiple notations:

Representation FormatExampleDescription
Dotted Decimal192.0.2.235Standard human-friendly format
Dotted Hexadecimal0xC0.0x00.0x02.0xEBEach octet represented in base-16
Dotted Octal0300.0000.0002.0353Each octet represented in base-8
Hexadecimal (no dots)0xC00002EBFull 32-bit value as a single hex number
Decimal (no dots)3221226219Full 32-bit value represented as decimal
Octal (no dots)030000001353Full 32-bit value represented as octal

👉 All of the above represent the same IPv4 address:

192.0.2.235


🧠 Why does this matter?

Some operating systems, browsers, and networking libraries accept alternative representations of IP addresses.
This can be exploited:

  • To evade security filters (firewalls, validation rules).

  • To bypass URL allow/block lists (e.g., app allows only whitelisted domain IP).

Example:

http://0xC00002EB → interpreted internally as 192.0.2.235

✅ Java Program — Normalize any IPv4 Representation

The following Java program accepts an IPv4 address in any supported notation (hex, octal, dotted, decimal), normalizes it, and prints it back in standard dotted decimal format.

package com.pretech; import java.net.URL; import java.util.StringTokenizer; import java.util.regex.Pattern; public class Ipv4Example { private static final Pattern IPV4REGEX = Pattern.compile( "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b" ); public static void main(String[] args) { try { System.out.println(createHostAddress("http://192.0.2.235")); System.out.println(createHostAddress("http://0xC0.0x00.0x02.0xEB")); System.out.println(createHostAddress("http://0300.0000.0002.0353")); System.out.println(createHostAddress("http://0xC00002EB")); System.out.println(createHostAddress("http://030000001353")); } catch (Exception e) { e.printStackTrace(); } } private static String createHostAddress(final String url) throws Exception { URL urldetails = new URL(url); String addr = urldetails.getHost(); boolean validHost = true; StringBuffer hostStringBuffer = new StringBuffer(); try { StringTokenizer hostTokenizer = new StringTokenizer(addr, "."); int tokenCount = hostTokenizer.countTokens(); // Case 1: nondotted hex or decimal number format if (isNumber(addr) && tokenCount == 1) { long decimalIpAddress = Long.decode(addr); hostStringBuffer.append(longToIpAddress(decimalIpAddress)); if (!IPV4REGEX.matcher(hostStringBuffer.toString()).matches()) { validHost = false; } // Case 2: dotted hex or dotted octal representation } else if (isNumber(addr) && tokenCount > 1) { int i = 0; while (hostTokenizer.hasMoreTokens()) { String token = hostTokenizer.nextToken(); hostStringBuffer.append(Integer.toString(Integer.decode(token))); if (i < 3) { hostStringBuffer.append("."); } i++; } if (!IPV4REGEX.matcher(hostStringBuffer.toString()).matches()) { validHost = false; } } else { // Other host formats hostStringBuffer.append(addr); } if (!validHost) { throw new Exception("Invalid Host name"); } } catch (Exception e) { throw new Exception("Invalid Host name: " + e.getMessage(), e); } return hostStringBuffer.toString(); } // Convert long format to dotted decimal IP public static String longToIpAddress(long ipAddress) { StringBuilder ipStringBuffer = new StringBuilder(); for (int i = 0; i < 4; i++) { ipStringBuffer.insert(0, Long.toString(ipAddress & 0xff)); if (i < 3) { ipStringBuffer.insert(0, '.'); } ipAddress >>= 8; } return ipStringBuffer.toString(); } private static boolean isNumber(final String addr) { try { StringTokenizer addrTokenizer = new StringTokenizer(addr, "."); while (addrTokenizer.hasMoreTokens()) { String token = addrTokenizer.nextToken(); Long.decode(token); // detects hex (0x..), octal (0..), decimal } return true; } catch (Exception e) { return false; } } }

🖨 Output

192.0.2.235 192.0.2.235 192.0.2.235 192.0.2.235 192.0.2.235

No comments:

Post a Comment

12 classic String-based Java interview questions with simple explanations and code.

  1️⃣ Check if a String is a Palindrome Problem Given a string, check if it reads the same forward and backward. Example: "madam...

Featured Posts