alpjose
asked on
IPAddress conversion from String(192.168.1.11) to Byteorderform
Hi,
Could i know how to convert IPAddress from String(192.168.1.11) to Byteorderform.
regards,
alpjose
Could i know how to convert IPAddress from String(192.168.1.11) to Byteorderform.
regards,
alpjose
Have a look at java.net.InetAddress:
public static InetAddress InetAddress.getByName()
public byte[] InetAddress.getAddress()
public static InetAddress InetAddress.getByName()
public byte[] InetAddress.getAddress()
check this code.
try
{
byte[] thisBytes = java.net.InetAddress.getBy Name("192. 168.1.11") .getAddres s();
for(int i = 0; i < thisBytes.length; i++)
System.out.println(thisByt es[i]);
}
catch(UnknownHostException ex)
{
}
Bye,
Naeem Shehzad Ghuman
try
{
byte[] thisBytes = java.net.InetAddress.getBy
for(int i = 0; i < thisBytes.length; i++)
System.out.println(thisByt
}
catch(UnknownHostException
{
}
Bye,
Naeem Shehzad Ghuman
>>to Byteorderform.
As what Java type?
As what Java type?
ASKER
For the IPAddress 192.168.1.11 , the byte order address is 2147483647.
This is Network Byte order.
This is Network Byte order.
You mean you want it as an integer?
ASKER
Yes i want it as integer.
have u tried my code?
try
{
byte[] thisBytes = java.net.InetAddress.getBy Name("192. 168.1.11") .getAddres s();
for(int i = 0; i < thisBytes.length; i++)
System.out.println(thisByt es[i]);
}
catch(UnknownHostException ex)
{
}
Bye,
Naeem Shehzad Ghuman
try
{
byte[] thisBytes = java.net.InetAddress.getBy
for(int i = 0; i < thisBytes.length; i++)
System.out.println(thisByt
}
catch(UnknownHostException
{
}
Bye,
Naeem Shehzad Ghuman
ASKER
yes i tried, but i am not getting the Network Byte order.
>>
the byte order address is 2147483647.
This is Network Byte order.
>>
Are you certain about that?
the byte order address is 2147483647.
This is Network Byte order.
>>
Are you certain about that?
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!
And JAVA is completely Network Byte Order internally.
You might reverse the sequence in the code above .....
;JOOP!
ASKER
I am not very sure,
i have used the below code to get the Value.
int getAddressInByteOrder(Stri ng strIPAddress) {
StringTokenizer st = new StringTokenizer(strIPAddre ss, .);
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.
i have used the below code to get the Value.
int getAddressInByteOrder(Stri
StringTokenizer st = new StringTokenizer(strIPAddre
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.
DO NOT USE Math.pow() WHEN DEALING WITH INTEGERS!
;JOOP!
;JOOP!
>>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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
>>let me know how to get definitive ip.
As in my code above ;-) That's for network byte order
As in my code above ;-) That's for network byte order
http://www.dotgnu.org/pnetlib-doc/System/Net/IPAddress.html