my $s = '0x010203';; ## Skip over the '0x' and extract the next 4 bytes ## then pack those 4 hex characters to binary my $bin = pack 'H*', unpack 'xxA4', $s;; ## unpack the binary back to hex to check it worked. print unpack 'H*', $bin;; 0102