On Sun, Nov 17, 2002 at 02:04:57PM +0700, budsz wrote:
> On Sat, Nov 16, 2002 at 04:21:34PM +0000, Matthew Seaman wrote:
> >expressed as a hexadecimal or even decimal integer. thus:
> >
> > 192.168.100.1 is the same as 0xc0a86401 or 3232261121
> > ^^^^^^^^^^
> >and
> >
> > 0xffffff00 is the same as 255.255.255.0 or 4294967040
> ^^^^^^^^^^
>
> Sorry sir, How we calculate that number (Decimal Interger)?, I hope
> explaination step by step?
Simple enough:
This perl snippet will convert a dotted quad address into an integer:
# in the spirit of inet_aton(3) convert an address given as a dotted
# quad into an integer
sub inet_atoi ($) {
my $ipaddr = shift;
my @bytes;
@bytes = (
$ipaddr =~ /^(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\.
(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\.
(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])\.
(25[0-5]|2[0-4][0-9]|[01][0-9][0-9]|[0-9][0-9]|[0-9])$/x
);
$ipaddr = 0;
for (@bytes) {
$ipaddr <<= 8;
$ipaddr += $_;
}
return $ipaddr;
}
There's a standard version of inet_aton() provided by the Socket
module (perldoc Socket), but that returns a packed string rather than
an integer value.
These three will print it out an integer in whatever appropriate
format:
# in the spirit of inet_ntoa(3) convert a network address expressed as
# an integer into the typical dotted-quad representation.
sub inet_itoa ($) {
my $ipaddr = shift;
my @bytes;
for ( 1 .. 4 ) {
unshift @bytes, $ipaddr & 0xff;
$ipaddr >>= 8;
}
return sprintf "%u.%u.%u.%u", @bytes;
}
# Display address as hex string
sub inet_itox ($) { return sprintf "%#x", $_[0]; }
# Display address as decimal integer
sub inet_itod ($) { return sprintf "%u", $_[0]; }
If you're not into perl programming, I wrote a little CGI or PHP
network calculator which you can slap into a website:
http://www.infracaninophile.co.uk/nwc/networkcalc-1.0.tar.bz2
Cheers,
Matthew
--
Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks
Savill Way
Marlow
Tel: +44 1628 476614 Bucks., SL7 1TH UK
To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-questions" in the body of the message