Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IPAddress conversion from String(192.168.1.11) to Byteorderform

Posted on 2004-10-05
20
Medium Priority
?
337 Views
Last Modified: 2008-03-17
Hi,

 Could i know how to convert IPAddress from String(192.168.1.11) to Byteorderform.

regards,
alpjose
0
Comment
Question by:alpjose
[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
  • 6
  • 5
  • 2
  • +4
20 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12224379
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12224397
Have a look at java.net.InetAddress:

public static InetAddress InetAddress.getByName()
public byte[] InetAddress.getAddress()

0
 
LVL 6

Expert Comment

by:expertmb
ID: 12224577
0
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!

 
LVL 5

Expert Comment

by:Naeemg
ID: 12224581
check this code.

    try
    {
      byte[] thisBytes = java.net.InetAddress.getByName("192.168.1.11").getAddress();
      for(int i = 0; i < thisBytes.length; i++)
        System.out.println(thisBytes[i]);
    }
    catch(UnknownHostException ex)
    {
    }
 

Bye,
Naeem Shehzad Ghuman
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12224584
>>to Byteorderform.

As what Java type?
0
 

Author Comment

by:alpjose
ID: 12224794
For the IPAddress 192.168.1.11 ,  the byte order address is 2147483647.
This is Network Byte order.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12224807
You mean you want it as an integer?
0
 

Author Comment

by:alpjose
ID: 12224816
Yes i want it as integer.
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12224824
have u tried my code?

try
    {
      byte[] thisBytes = java.net.InetAddress.getByName("192.168.1.11").getAddress();
      for(int i = 0; i < thisBytes.length; i++)
        System.out.println(thisBytes[i]);
    }
    catch(UnknownHostException ex)
    {
    }
 

Bye,
Naeem Shehzad Ghuman
0
 

Author Comment

by:alpjose
ID: 12224836
yes i tried, but i am not getting the Network Byte order.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12224932
>>
the byte order address is 2147483647.
This is Network Byte order.
>>

Are you certain about that?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12224994
An XDR decoding should give you byte order.
And JAVA is completely Network Byte Order internally.
You might reverse the sequence in the code above .....
;JOOP!
0
 

Author Comment

by:alpjose
ID: 12224998
I am not very sure,

i have used the below code to get the Value.

int getAddressInByteOrder(String strIPAddress) {
            StringTokenizer st = new StringTokenizer(strIPAddress, .);
            int m = 256, n = 1, o = 2, p = 3;
            int a1 = 0, b1 = 0, c1 = 0, d1 = 0;
            while (st.hasMoreElements()) {
                  a1 = Integer.parseInt((String) st.nextElement());
                  b1 = Integer.parseInt((String) st.nextElement());
                  c1 = Integer.parseInt((String) st.nextElement());
                  d1 = Integer.parseInt((String) st.nextElement());
            }
            int com = 0;
            com += (d1 * Math.pow(m, p));
            com += (c1 * Math.pow(m, o));
            com += (b1 * Math.pow(m, n));
            com += (a1);
            return com;
      } // ending getAddressInByteOrder

I got the required result for 192.168.1.x range , but for some range of Address i am not getting the correct value.

for the range 134.27.57.127, i am supposed to get -197583994 but i am getting -2147483515.
So i would like to know if there is any other way to do this.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12225018
DO NOT USE Math.pow() WHEN DEALING WITH INTEGERS!
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12225057
>>for the range 134.27.57.127,

It's not a range - it's an address ;-)

>>i am supposed to get -197583994

I don't think that's right

>>but i am getting -2147483515.

and i don't think that is either.

Try to get a definitive ip to int so it can be replicated/tested
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 12225092
This is the simplest way:

     InetAddress ip = InetAddress.getByName("134.27.57.127");
     byte[] bytes = ip.getAddress();
     int address = new java.math.BigInteger(bytes).intValue();
     System.out.println(address);
0
 

Author Comment

by:alpjose
ID: 12225101
Pls let me know how to do.
I mean to say 134.27.57.127 's range ie  134.27.57.128...
Fine for this IP , let me know how to get definitive ip.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12225130
>>let me know how to get definitive ip.

As in my code above ;-) That's for network byte order
0

Featured Post

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.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

636 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