Solved

Returning negetive IP address if any octet > 128

Posted on 2006-06-14
10
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

863 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

23 Experts available now in Live!

Get 1:1 Help Now