You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Poloha Peter (ext)" <Pe...@ts.siemens.de> on 2004/08/11 10:49:54 UTC
RE: [off topic] IP String to Integer function?
check the standard java package java.net, class InetAddress etc...
peter
> -----Original Message-----
> From: Guy Katz [mailto:gkatz@allot.com]
> Sent: Mittwoch, 11. August 2004 11:44
> To: 'Jakarta Commons Users List'
> Subject: [off topic] IP String to Integer function?
>
>
> hi all;
> i am looking for a function shich converts an IP string
> (xxx.xxx.xxx.xxx) to
> an Integer.
> is there such a function available somewhere?
> thanks,
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
Re: [off topic] IP String to Integer function?
Posted by "Mark R. Diggory" <md...@latte.harvard.edu>.
I have a chunk of code to do this, hope it helps:
public class IPAddress {
/** Holds value of property ip. */
protected int address;
/** Creates new IP */
public IPAddress(String ip) throws MalformedIPException{
this.set(ip);
}
/** Getter for property ip.
* @return Value of property ip.
*/
public int get(){
return address;
}
/** Setter for property ip.
* @param ip New value of property ip.
*/
public void set(String ip) throws MalformedIPException{
if( ip == null) ip = "255.255.255.255";
int IP = 0x00;
int hitDots = 0;
char[] data = ip.toCharArray();
for(int i = 0; i < data.length; i++) {
char c = data[i];
if (c < 48 || c > 57) { // !digit
throw new MalformedIPException("IP Contains Non-digit");
}
int b = 0x00;
while(c != '.') {
if (c < 48 || c > 57) { // !digit
throw new MalformedIPException("IP Contains Non-digit");
}
b = b*10 + c - '0';
if (++i >= data.length)
break;
c = data[i];
}
if(b > 0xFF) { /* bogus - bigger than a byte */
throw new MalformedIPException("Value is larger than a byte");
}
IP = (IP << 8) + b;
hitDots++;
}
if(hitDots != 4 || ip.endsWith(".")) {
throw new MalformedIPException("Not properly formated IP address");
}
address = IP;
}
public String toString(){
return ((address >>> 24) & 0xFF) + "." +
((address >>> 16) & 0xFF) + "." +
((address >>> 8) & 0xFF) + "." +
((address >>> 0) & 0xFF);
}
public boolean contains(Object obj) {
if (obj == null) return false;
if (obj instanceof IPAddress)
return (((IPAddress)obj).address == address);
return false;
}
/**
* Returns a hashcode for this IP address.
*
* @return a hash code value for this IP address.
*/
public int hashCode() {
return address;
}
}
Michael McGrady wrote:
> At 01:49 AM 8/11/2004, you wrote:
>
>> check the standard java package java.net, class InetAddress etc...
>> peter
>
>
>
> I have not checked what the result is, but I do know that InetAddress
> overides hashcode() in Object.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
--
Mark R. Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org
RE: [off topic] IP String to Integer function?
Posted by Michael McGrady <mi...@michaelmcgrady.com>.
At 01:49 AM 8/11/2004, you wrote:
>check the standard java package java.net, class InetAddress etc...
>peter
I have not checked what the result is, but I do know that InetAddress
overides hashcode() in Object.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org