I have 256 character strings of hexadecimal characters that represent a sequence of bit flags, and I'm trying to convert them back to bitstring so that I can control them with & , | , vec and the like. Hexadecimal strings are written in integer wide-angle groups, so a group of 8 bytes, such as "76543210" , should translate to the bitstron "\x10\x32\x54\x76" , that is, the lower 8 bits are 00001000 .
The problem is that the pack " h " format works on one input byte at a time, and not on 8, so the results from using it directly will not be in the correct order. At the moment I am doing this:
my $bits = pack("h*", join("", map { scalar reverse $_ } unpack("(A8)*", $hex)));
which works but feels hacked. It seems like there should be a cleaner way, but my pack -fu is not very strong. Is there a better way to do this translation?
source share