in reply to Testing & Using Bits

OK, so maybe writing a perl script over the holiday WASN'T my greatest idea, but...(the honey-do list not withstanding) I had a super time. Now it's back to questions... FunkyMonk- I tried your suggestion & had some success, but it seems I don't get the proper result when I try to use something like my $x = ob01001001. I'm on a windows XP box, would that make a difference? I'm trying to print a 12 byte UPC Code from a 6 byte BCD field. I think I understand the shifting (>>) of the 4 bits, but when I try $hi=($x & 0b11110000).. 4; I think I need to "OR" it with 0x30 inorder to get the ASCII digit. ie 0101 0000 shifted = 0000 0101, then an ASCII digit of 5 would actually be 0x35 or 00110101, am I way off here? Then I would join the 12 bytes into a string for printing. When I OR it all I end up with is the 0x30 or 48dec. Thanks for the help, & your patience. BTW I have 2 perl books - the Gecko & the Black Panther. I'm thinking I neeed one in between. Does anyone recomend the camel vs llama books (I can only afford ONE of them right now.) Thanks.

Replies are listed 'Best First'.
Re^2: Testing & Using Bits
by FunkyMonk (Bishop) on May 29, 2007 at 09:21 UTC
    Perl is very flexible with its conversions between numbers and text so there aren't any conversions to ASCII needed:

    my $x = 0b01001001; my $hi = ($x & 0b11110000) >> 4; $hi = $hi . " (four)"; print "The answer was $hi"; #The answer was 4 (four)

    As far as a book recommendation goes, I learnt Perl mostly from The Perl Cookbook. A great book if you have some experience of other languages.

    Perl comes with a huge amount of help for learning Perl. If you're running Activestate Perl, you should have all the documentation in html format already installed. Have a poke around in the Start menu. Of course, PerlMonks has a wealth of information too. Have a look in the tutorials section.

    If you're going to spend any time here at PM, I think you should read Writeup Formatting Tips ;)