in reply to Re: Hex to Octet
in thread Hex to Octet

Thank you,
That is what I was looking for, now if I wanted to do the reverse should I substitute unpack for pack?

Thank again

Replies are listed 'Best First'.
Re: Re: Re: Hex to Octet
by Mr. Muskrat (Canon) on Sep 09, 2003 at 18:40 UTC

    I built upon sgifford's code.

    #!/usr/bin/perl my $hex = "0xb400189"; # the 0x is optional since we call hex later my $octet = hex2octet($hex); print $octet, "\n"; $hex = octet2hex($octet); print $hex, "\n"; sub hex2octet { my $hex = shift; my @octet = unpack("C4", pack("N", hex $hex)); return sprintf "%x.%x.%x.%x", reverse @octet; } sub octet2hex { my $octet = shift; my @octet = map { hex } split /\./, $octet; my $hex = unpack("N", pack("C4", reverse @octet)); return sprintf "%x", $hex; }

      Excellent!

      Thanks guys