Returning negetive IP address if any octet > 128

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.


sanjoybasuAsked:
Who is Participating?
 
StillUnAwareConnect With a Mentor Commented:
Change lines like this:

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

to

  ipFormat = ipFormat.append(ipAddr[0] & 0xFF);
0
 
CEHJCommented:
pFormat = ipFormat.append(ipAddr[0] & 0xFF);
0
 
runa_paathakCommented:
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.

 
StillUnAwareCommented:
> 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
 
StillUnAwareCommented:
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
 
sanjoybasuAuthor Commented:
StillUnAware ,
Thanks can you  please explain me how AND operation with FF fix the problem.
0
 
CEHJCommented:
>>ipFormat.append(ipAddr[i]).append('.');

should be

ipFormat.append(ipAddr[i] & 0xFF).append('.');
0
 
StillUnAwareCommented:
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
 
CEHJCommented:
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
 
sanjoybasuAuthor Commented:
Thanks  CEHJ & StillUnAware  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.