Hi Monks, I've been having a really hard time getting this code to work right and I'm betting you'll see instantly what I've done wrong. I'm passing an integer representation to a script, packing it as a binary string and then and'ing against a binary switch stored in a hash. However when it gets to the portion that does the &'ing for some reason it appears to be treating the binary packed string as an integer value and is decoding wrong
my %SWITCH = ( CAT => 0b00001, DOG => 0b00010, HAMSTER => 0b00100, FERRIT => 0b01000, FISH => 0b10000); my $petInput = 2; my @PETS; my $binPet = unpack("B32", pack("N", $petInput)); $binPet =~ s/^0+(?=\d)//; # gets rid of leading zeros printf "%s%bn%s", "\n", $binPet, "\n"; # at this point $binPet unfortunately contains "0b1010" # rather than 0b10 as expected, so I'm doing something wrong # in my pack/unpack apparently for (keys %SWITCH) { if ($binPet & $SWITCH{$_}){push (@PETS, $_);} } foreach (@PETS){print $_."\n";}
This results in DOG, FERRIT when it should just be DOG. What am I missing here? Thank You Monks!!!!
In reply to having trouble unmasking a binary string by emoss1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |