[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: aside: perl's unpack()



There are those who would have you believe that Damien wrote:
> > > I can tell you an easier way to decipher them, but I too would like to
> > > know how they are contructed, i.e what's the algorithm.
> > 
> > a.b.c.d
> > 
> > n = d + c * 256 + b * 256 ^ 2 + a * 256 ^ 3
> 
> after reading the previous mail, this is what i was thinking. i went to test
> it in perl afterwards, but got the wrong result. could anyone tell me what's
> wrong with my interpretation?
> 
> perl -le '$, = "."; print unpack("C4", "2704935062");'
> 


IP networks use big-endian notation and PCs use little-endian; you
need to convert the value before splitting it back into octets: 


($a, $b, $c, $d) = split(/\./, "50.55.48.52");
$n = $d + ($c << 8) + ($b << 16) + ($a << 24);
print "$n = ", join(".", unpack("C4", pack("N", $n))), "\n";


I'll leave it as an exercise to make this into a one-liner ;-).



Reply to: