Reputation: 297
I have a script that does a bunch of calculations on IPV4 and IPV6 address's in their hex form
for example "70.02.03.04" would be
$ipv4="\x46\x02\x03\x04"
This binary handle is hardcoded in my script. But now I want to generate this on the fly
I'm using "sprintf("x%02x", $value) for hex values, but how would I generate the binary handle as above?
Upvotes: 0
Views: 80
Reputation: 386646
You don't want hex (a string representation of a number).
my $hex = '46020304';
You want the number as a four byte machine integer.
my $uint32 = "\x46\x02\x03\x04";
You can achieve this using
my $packed = pack('C4', split /\./, '70.02.03.04');
and with
use Socket qw( inet_aton );
my $packed = inet_aton('70.02.03.04');
Upvotes: 2