in reply to Testing & Using Bits

FunkyMonk, thanks for the insite- I read the posting tips & appreciate the heads up. My last post WAS rather ugly. My problem seems to be threefold-
1. Finding things in the man pages. Where is the Bits:: etc. functions defined & do I need use statements at the beginning of my code to use things like Bits:: or File:: etc.?
2. I've been learning new languages for over 3 decades & it grinds me to have to ask someone else to do my work, but alas "seeking devine guidence", can save time. Thanks for the tip on the tutorials. There was a segment that I stumbled onto that caused me some grief...
print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x123 +45678))), "\n"; print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",$Byte +s))), "\n";
This first line works, it prints the 0x12 0x34 etc., but the second just prints 0x00 0x00 etc. I know the 6 hex bytes are in $Bytes because printing it yeilds the proper graphics on the screen. Can you explain the "%#02x"? I've read the printf doc but the light just isn't comming on.
3. See, sorry to ramble on, but when I finally do ask for help, I just want to give you as much info as possible. I work in support, so I know all too well the headaches associated with getting bits (no pun) of info on a "need to know" basis. I guess the next post will have to be "a sample of my script", but I'm trying to avoid burdening you with the laughter. Thanks again.
By the way, I don't plan on spending time here, but if you folks would prefer I sign up, I'd be happy to. At my age, I just try to avoid having to remember additional IDs & passwords. Thanks.
'le Dufois.

Replies are listed 'Best First'.
Re^2: Testing & Using Bits
by FunkyMonk (Bishop) on May 31, 2007 at 17:46 UTC
    Your code works here:

    my $bytes = 0x123456; print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",0x123 +45678))), "\n"; print join(" ", map { sprintf "%#02x", $_ } unpack("C*",pack("L",$byte +s))), "\n"; #output: #0x78 0x56 0x34 0x12 #0x56 0x34 0x12 00

    What did you expect it to do?

      That's exactly what I expected. I'll recheck my code, but I was getting all zeroes. I even cut/pasted the line & THEN substituted the scalar for the string. Also where can I fine the doc on the sprintf codes %#02x, etc. Thanks.
        %#02x means:

        • % A format code
        • # prefix with "0x"
        • 02 2 digits wide, padded with a leading zero if necessary
        • x hexadecimal

        You can read the full documentation here.

        OK, I guess there's just no getting around it... ;-) Here's my code...
        # !perl DebugBCD.pl open (inFILE, "<C:/Dev/Perl_Dev/ItemExp/TESTFILE.DAT") || die "Cannot open file\n"; # The first 6 bytes of my test file contain the 12 digit UPC Barcode 1 +23456789910 binmode (inFILE); seek (inFILE,0,0); read (inFILE,$inBuf,6); print "Len=".length($inBuf)." Raw Data = $inBuf \n"; # This prints t +he graphics $Test = DecodeBCD($inBuf); print $Test; # This prints 48 48 52 48 I expected + 1 2 3 4 close inFile; sub DecodeBCD { my ($i,@lo,@hi,$Byte,$Bits,@pairs); my $Hi,$Low; my $LoMask = 15; my $HiMask = 240; my $Zone = 0x30; for ($i = 0; $i < 2; $i++) { $Byte = substr($_[0],$i,1); $lo[$i] = ($Byte & $LoMask) | $Zone; # These OR statement +s seem to $hi[$i] = (($Byte & $HiMask) >> 4) | $Zone; # result in JUST the + $Zone piece, $pairs[$i] = join("",$lo[$i],$hi[$i]); # Implying that the +& result is Zero. print "\nLo $i=".$lo[$i]." Hi $i=".$hi[$i]." $pairs[$i]\n +"; } print "(@pairs)\n"; chomp ($Dummy = <STDIN>); join('',@pairs); }
        Please try to keep the laughter to a dull roar.