Hello monks, I have a problem with perl's pack function and null byte padding. Here's the problem:
print unpack "H*", pack("H*", "a12");
This returns a120, because packed data is padded to full byte length at the end, not at the front.
This is quite problematic. Possible solution is to do this instead
my $string = "a12";
print unpack "H*", pack("H*", "0" x (length $string % 2) . $string);
Of course this is a lot uglier, requires you to actually have a variable declared and is not as explicit as the first version, however it works.
My question is: is there a possibility to do the same thing with just pack format string to avoid patching it with length() of the string? Is there a cpan library that takes care of this? I wrote my own helper function for this but I've run into some circullar using of my packages which I want to avoid.
Help appreciated!