Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

having trouble unmasking a binary string

by emoss1 (Initiate)
on Mar 28, 2013 at 02:10 UTC ( #1025835=perlquestion: print w/ replies, xml ) Need Help??
emoss1 has asked for the wisdom of the Perl Monks concerning the following question:

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!!!!

Comment on having trouble unmasking a binary string
Download Code
Replies are listed 'Best First'.
Re: having trouble unmasking a binary string
by hdb (Prior) on Mar 28, 2013 at 08:42 UTC

    Why do you need all this strange logic? This works fine I think:

    my %SWITCH = ( CAT => 0b00001, DOG => 0b00010, HAMSTER => 0b00100, FERRIT => 0b01000, FISH => 0b10000); my $petInput = 2; my @PETS; for (keys %SWITCH) { if ($petInput & $SWITCH{$_}){push (@PETS, $_);} } foreach (@PETS){print $_."\n";}
Re: having trouble unmasking a binary string
by Cristoforo (Deacon) on Mar 28, 2013 at 03:09 UTC
    At this point
    $binPet =~ s/^0+(?=\d)//;
    $binPet contains the string '10' and then when you print out that string in
    printf "%s%bn%s", "\n", $binPet, "\n";, it gives the binary representation of ten, (1010), not a binary '2', (0b10).

    And I don't know the purpose of 'n' in the printf format string, "%s%bn%s".

    Beyond that, I'm not sure of what to tell you - the purpose of the code.

Re: having trouble unmasking a binary string
by Not_a_Number (Parson) on Mar 28, 2013 at 12:33 UTC

    No need to unmask anything:

    my %SWITCH = ( CAT => 0b00001, DOG => 0b00010, HAMSTER => 0b00100, FERRIT => 0b01000, FISH => 0b10000); my $petInput = 2; my @PETS; for ( keys %SWITCH ) { push @PETS, $_ if $SWITCH{$_} == $petInput; } print "$_\n" for @PETS; # DOG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025835]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (19)
As of 2015-07-31 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (278 votes), past polls