extract IP address (aaa.bbb.ccc.ddd) from byte array

Can you help me to extract IP v4 format from a byte array?

I have an array of bytes, (I even have a function that converts it to an integer) but I'd like to get aaa.bbb.ccc.ddd representation out of it...

Please help if you can.

Thanks Much,

Message was edited by:

choghok

[312 byte] By [choghoka] at [2007-11-26 15:23:32]
# 1
Check out the InetAddress class API documentation.kind regards,Jos
JosAHa at 2007-7-8 21:38:48 > top of Java-index,Java Essentials,Java Programming...
# 2

[nobr]Recently I find that I needed that also, and look out on the web, and I got the

method that returns the byte array, but as you may have seen, as it's base is 7,

you don't get numbers greater than 127, so you get negative numbers, probably

your method adds 255 to the negative numbers to get the right number.

but if you want the aaa.bbb.ccc.ddd, i did a method for myself but that could help you.

/**This method returns a String array which contains the IP address and name of the localhost.<br>

* Values are returned as:

* <ol>

*<li>position[0] = IP address</li>

*<li>position[1] = name</li>

* </ol>

* <br>If Uknown Host, <b>null</b> is returned;<br>

* @return the ip addres and name of the localhost machine.

*/

public static String[] getLocalhostIPandAddress(){

try {

InetAddress addr = InetAddress.getLocalHost();

String ipAddr = addr.getHostAddress();//Get IP Address

String hostname = addr.getHostName();//Get hostname

return new String[]{ipAddr, hostname};

} catch (UnknownHostException e) {

return null;

}

}

Good luck, hope this helps.[/nobr]

@lphazygmaa at 2007-7-8 21:38:48 > top of Java-index,Java Essentials,Java Programming...