Sorry, my previous reply was a bit of a quickie. I did some experiments and wanted to now more explicitly fill in some of the bits that you and I hinted at.
Your original code and my modified version here:
both work for any list of valid IP addresses in dotted-decimal notation, even those above 128.0.0.0.my @range= unpack "N*", pack "C*", split /[. ]+/, "@ARGV";
The problem comes when you try to do:
with such large numbers. That causes Perl to squawk:$range[0]..$range[1]
Which was news to me; thanks for teaching me something. (:Range iterator outside integer range at ...
Your solution of using 0..$range[1]-$range[0] is quite good. You could also do:
Both should work for even 56-bit integers (I don't recall IPv6 address notation...). Though, you can't use the "N" format for pack()/unpack() for more than 32 bits, so you'd need to build up the number:for( $_= $range[0]; $_ <= $range[1]; $_++ )
Note that using bit-wise operators (or use integer) above would break these. (I think using push instead of unshift and adding a reverse might be slightly more efficient.)sub octets2num { my $val= 0; for( split /\./, shift(@_) ) { $val= 0x100*$val + $_; } return $val; } sub num2octects { my $val= shift(@_); my @bytes; while( 0 < $val ) { unshift @bytes, $val%0x100; $val= int( $val / 0x100 ); } return ! @bytes ? "0" : join ".", @bytes }
Update: I couldn't help golfing the first line of code a bit.
- tye (just playing; that's how I usually learn)In reply to (tye)Re2: BigInt usage
by tye
in thread BigInt usage
by ryan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |