Solved

Returning negetive IP address if any octet > 128

Posted on 2006-06-14
10
266 Views
Last Modified: 2010-03-31
Folks,
I am pretty new to java programming so excuse me if this question looks silly to you.

I wrote a code which is returning negetive value of IP octet if octet is greater than 128
For example  actual IP address is

Non-authoritative answer:
Name:   www.colorado.edu
Address: 128.138.129.98

But my code returns -128.-118.-127.98

Now let see another example
Non-authoritative answer:
Name:   www.sun.com
Address: 72.5.124.61

My code returns 72.5.124.61

Here is the part of my code

            byte[] remIp=new byte[4];
            String remHost=null;
            try {
                  InetAddress remNet=InetAddress.getByName("www.sun.com" ); // Actual code there is String Variable instead of www.sun.com //
                  remIp=remNet.getAddress();
                  remHost=remNet.getHostName();
                  
            }catch (UnknownHostException uhe){
            uhe.printStackTrace();      
            }
            System.out.println("IP address of " +remHost +" is " +printIp(remIp));

 printIp is a method I have written to put it in correct IP format. Here goes the code...

public static String printIp(byte[] ipAddr){
            StringBuffer ipFormat=new StringBuffer();
             ipFormat = ipFormat.append(ipAddr[0]);
             ipFormat = ipFormat.append(".");
             ipFormat = ipFormat.append(ipAddr[1]);
             ipFormat = ipFormat.append(".");
             ipFormat = ipFormat.append(ipAddr[2]);
             ipFormat = ipFormat.append(".");
             ipFormat = ipFormat.append(ipAddr[3]);
             return ipFormat.toString();
            
            
      }

Initially I thought " printIp" could be the problem  so I just tried to print each element of byte[] remIp=new byte[4] but looks like  thats not the problem.


0
Comment
Question by:sanjoybasu
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Accepted Solution

by:
StillUnAware earned 250 total points
ID: 16900364
Change lines like this:

  ipFormat = ipFormat.append(ipAddr[0]);

to

  ipFormat = ipFormat.append(ipAddr[0] & 0xFF);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900365
pFormat = ipFormat.append(ipAddr[0] & 0xFF);
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16900374
Use int[] instead of byte[]
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16900391
> Use int[] instead of byte[]

That's a bad idea, cause it will take more data, and if You'll have to send the address as a binary 4 byte number You'll have to convert the int's to bytes back and forth.
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16900420
Actually the best practice while using StringBuffer (or StringBuilder since Java 1.5), would be:

public static String printIp(byte[] ipAddr){
    StringBuffer ipFormat=new StringBuffer(16);
    for(int i = 0; i < ipAddr.length; i++)
        ipFormat.append(ipAddr[i]).append('.');
    return ipFormat.toString();
}
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sanjoybasu
ID: 16900421
StillUnAware ,
Thanks can you  please explain me how AND operation with FF fix the problem.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900451
>>ipFormat.append(ipAddr[i]).append('.');

should be

ipFormat.append(ipAddr[i] & 0xFF).append('.');
0
 
LVL 14

Expert Comment

by:StillUnAware
ID: 16900462
Well, that's a tricky question, I'm not that fluent in english, either I am sure if I can explain it why ;)
All I can say, is that 0xFF is positive integer equal to 255, now when You and it with negative byte 0x80 (-127) the sign of it (7th bit) is interpreted as a value bit instead of sign bit. But that is rude and crude explanation, I'd recommend to read some real book on that :)

P.S. again I've made a little mistake while posting last comment, the code misses the functionality You've asked in the first place. Here comes the right one:

public static String printIp(byte[] ipAddr){
    StringBuffer ipFormat=new StringBuffer(16);
    for(int i = 0; i < ipAddr.length; i++)
        ipFormat.append(ipAddr[i] & 0xFF).append('.');
    return ipFormat.toString();
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16900478
A byte in Java is a signed value. You can't hold positive values over 127, so the value has to be reinterpreted by 'unsigning' it. This is done by ANDing with 0xFF which causes a promotion to int, which *can* hold the greater values between 128 and 255 inclusive
0
 

Author Comment

by:sanjoybasu
ID: 16900597
Thanks  CEHJ & StillUnAware  
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now