Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This code:

if ($test1 = $test2) { $comp = "EQUALS"; } else { $comp = "Does NOT Equal"; }

does not do what you think it does. Most likely you want to use one of the comparison operators, eq for string comparison or == for numerical comparison. Look at:

my $test1 = 123; my $test2 = 456; print "1. Test1: '$test1', test2: '$test2'\n"; if ($test1 = $test2) { print "2a. Test1: '$test1', test2: '$test2'\n"; $comp = "EQUALS"; } else { print "2b. Test1: '$test1', test2: '$test2'\n"; $comp = "Does NOT Equal"; } print "3. Test1: '$test1', test2: '$test2'\n";

I use the following to convert a BCD string to a Perl number/string:

=head2 C<decode_COMP3> Decodes a COMP3 BCD-number with trailing sign =cut sub decode_COMP3 { # Cut off the last nibble # 0C -> + # 0D -> - # 0F -> (unsigned number) my $digits = unpack "H*", $_[0]; my $sign = chop $digits; #print "$digits\n"; if ($sign eq 'D' or $sign eq 'd') { $sign = '-' } elsif ($sign eq 'C' or $sign eq 'c') { $sign = '+' } elsif ($sign eq 'F' or $sign eq 'f') { $sign = ' ' } else { $digits .= $sign; $sign = '?' }; "$sign$digits" };

In your case, maybe you want to throw away all the sign handling. The code could then be:

sub decode_BCD { unpack "H*", $_[0]; };

When debugging string packing/unpacking problems, I often write me a test suite to make sure all my assumptions about the data get documented somewhere. You might want to use something like the following:

use strict; use Test::More tests => 4; use_ok 'MyBCDDecodePackage'; is decode_BCD("\x12\x34\x56","123456"); is decode_BCD("\x00\x34\x56","003456"); is decode_BCD("\x12","12");

and then add all cases from your actual input data that you find interesting/problematic.


In reply to Re: Encoding BCD by Corion
in thread Encoding BCD by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2014-09-19 00:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (129 votes), past polls