Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: Conversion from decimal to dotted decimal base 256 number
|
Posted: Apr 25, 2003 6:43 PM
|
|
/*
* IpFormatter.java
*
* Simple demo of converting an int to a IP-format string.
*
* Copyright 2003, Matt Gerrans.
*
* Turning in this code as homework is a violation of ethical principals,
* and punishable by severe penalties, even if you change the variable
* names. Feeding it to your dog is acceptible.
*/
class IpFormatter
{
static String getIpFormat( int number )
{
StringBuffer buffy = new StringBuffer();
for( int i = 0; i < 4; i++ )
{
int part = 0xff & (number >> ((3-i)*8));
buffy.append( i > 0 ? "." : "" );
buffy.append( Integer.toString(part));
}
return buffy.toString();
}
public static void main(String args[])
{
int N = 0xffffffff; // 255.255.255.255.
try
{
N = Integer.parseInt(args[0]);
}
catch( java.lang.ArrayIndexOutOfBoundsException aiobe )
{
}
catch( java.lang.NumberFormatException nfe )
{
System.out.println( "Syntax: " + "\n"
" java IpFormatter [N]" + "\n"
"Where N is an integer value." );
}
System.out.println( N + " -> " + getIpFormat(N) );
}
}
|
|