Solved

Returning negetive IP address if any octet > 128

Posted on 2006-06-14
10
272 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

830 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