regex - Issue with as_hex() function in perl -
i trying write code cidr ipv6.
basicall, getting cidr perfix in code , convert binary.
then using as_hex() function in bigint library, convert hexadecimal.
that works fine.
the problem when try hexadecimal inverted binary. how cant it, prints out white spaces.
prefix : 78
actual binary: 0b11111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000
hex: ffff:ffff:ffff:ffff:fffc:0000:0000:0000
inverted binary: 0b00000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111
hex: 3fff:ffff:ffff:f::::
any ideas????
i think need handle leading zeros, as_hex seems rid of default. using 2 binary sequences provided:
use math::bigint; sub paddedhex { ($binary) = @_; $x = substr(math::bigint->new($binary)->as_hex(), 2, -1); return sprintf("0x%032s\n", $x); } @binaries = qw/ 0b11111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000 0b00000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111 /; $binary (@binaries) { print paddedhex($binary), "\n"; }
i got output:
0x0fffffffffffffffffffc00000000000 0x000000000000000000003fffffffffff
hopefully helps?
Comments
Post a Comment