#!/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;
}
|