Solved

Returning negetive IP address if any octet > 128

Posted on 2006-06-14
10
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Better way to make a string with template variables in java 3 53
Setup GlassFish 4 57
jmeter usage 4 68
Java array 21 150
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

752 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