Solved

IPAddress conversion from String(192.168.1.11) to Byteorderform

Posted on 2004-10-05
20
296 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
  • 6
  • 5
  • 2
  • +4
20 Comments
 
LVL 18

Expert Comment

by:armoghan
Comment Utility
0
 
LVL 11

Expert Comment

by:cjjclifford
Comment Utility
Have a look at java.net.InetAddress:

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

0
 
LVL 6

Expert Comment

by:expertmb
Comment Utility
0
 
LVL 5

Expert Comment

by:Naeemg
Comment Utility
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
Comment Utility
>>to Byteorderform.

As what Java type?
0
 

Author Comment

by:alpjose
Comment Utility
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
Comment Utility
You mean you want it as an integer?
0
 

Author Comment

by:alpjose
Comment Utility
Yes i want it as integer.
0
 
LVL 5

Expert Comment

by:Naeemg
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

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

Expert Comment

by:CEHJ
Comment Utility
>>
the byte order address is 2147483647.
This is Network Byte order.
>>

Are you certain about that?
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
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
Comment Utility
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
Comment Utility
DO NOT USE Math.pow() WHEN DEALING WITH INTEGERS!
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>let me know how to get definitive ip.

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

771 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

11 Experts available now in Live!

Get 1:1 Help Now