Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:09:16+00:00 2026-05-31T10:09:16+00:00

I am checking whether the ipAddress is in Private Category or not. So I

  • 0

I am checking whether the ipAddress is in Private Category or not. So I wrote this method below. And I am getting this as an exception-

java.net.UnknownHostException: addr is of illegal length
    at java.net.InetAddress.getByAddress(InetAddress.java:948)
    at java.net.InetAddress.getByAddress(InetAddress.java:1324)

ipAddress (172.18.36.81) is String

if(isPrivateIPAddress(ipAddress)) {

            return null;
        }


private static boolean isPrivateIPAddress(String ipAddress) {

    byte[] byteArray = null;
    InetAddress ia = null;
    try {
        byteArray = ipAddress.getBytes("UTF-16LE");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    try {
        ia = InetAddress.getByAddress(byteArray);
    } catch (UnknownHostException e) {

        e.printStackTrace();
    }

    return ia.isSiteLocalAddress();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:09:17+00:00Added an answer on May 31, 2026 at 10:09 am

    I think you’ve misunderstood how to convert an IP address from String to byte[]. The proper way to do that is to parse String to a sequence of ints, and then cast each of those to a byte. But fortunately, InetAddress already has a method to handle that for you, so you can just write:

    private static boolean isPrivateIPAddress(String ipAddress)
    {
        return InetAddress.getByName(ipAddress).isSiteLocalAddress();
    }
    

    (together with whatever validity-checking and error-handling you want).

    Note that the above will also handle hostnames, by using DNS lookup. If you don’t want that, you’ll need to pre-check the IP-address, using something like this:

    if(! Pattern.matches("(\\d{1,3}\\.){3}\\d{1,3}", ipAddress)
        throw new IllegalArgumentException();
    

    if you’re O.K. with only supporting IPv4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for checking whether the Date is OK or not, but
Is this the right way of checking whether a directory is empty or not
Is there a definitive JavaScript method for checking whether or not a web page
I'm checking whether a file has been modified or not in order to refresh
Again I have problem with checking whether DataReader object has data or not? Dim
What's the simplest way of programatically checking whether or not a particular Windows machine
What would the code be for checking whether the Wi-Fi is enabled or not?
Another newbie question from me. Just checking whether my code below is possible. I.e
I'm checking whether internet is available or not URL url = new URL(http://www.google.co.in/); final
There are some patterns for checking whether a parameter to a method has been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.