melez has asked for the wisdom of the Perl Monks concerning the following question:
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 insteadprint unpack "H*", pack("H*", "a12");
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!my $string = "a12"; print unpack "H*", pack("H*", "0" x (length $string % 2) . $string);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pack uneven length hex
by choroba (Cardinal) on Nov 23, 2018 at 12:21 UTC | |
|
Re: Pack uneven length hex
by Eily (Monsignor) on Nov 23, 2018 at 09:59 UTC | |
by Anonymous Monk on Nov 24, 2018 at 06:39 UTC | |
by bliako (Abbot) on Nov 24, 2018 at 13:11 UTC | |
by melez (Sexton) on Nov 23, 2018 at 10:22 UTC | |
by syphilis (Archbishop) on Nov 23, 2018 at 10:50 UTC | |
by Eily (Monsignor) on Nov 23, 2018 at 10:57 UTC | |
|
Re: Pack uneven length hex
by james28909 (Deacon) on Nov 23, 2018 at 14:50 UTC | |
|
Re: Pack uneven length hex
by melez (Sexton) on Nov 23, 2018 at 21:51 UTC | |
|
Re: Pack uneven length hex
by ikegami (Patriarch) on Nov 26, 2018 at 10:01 UTC |