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

Re: aside: perl's unpack()



On Sun, Nov 05, 2000 at 08:46:05PM +1100, Damien wrote:
> > 
> > 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");'

Notice what you're getting: "50.55.48.52", which is ASCII code for
"2704". You have to convert the string "2704935062" into the integer
2704935062 in network byte order.

  perl -le 'print join(".", unpack("C4", pack("N", $ARGV[0])))' 2704935062

Going the other direction isn't too difficult, either:
  
  perl -le 'print unpack("N", pack("C4", split("\\.", $ARGV[0])))' 127.0.0.1


-- 
  finger for GPG public key.

Attachment: pgp2WiWlCJYgW.pgp
Description: PGP signature


Reply to: