Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Encoding BCD

by Corion (Pope)
on Jun 13, 2007 at 15:49 UTC ( #621007=note: print w/ replies, xml ) Need Help??


in reply to Encoding BCD

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.


Comment on Re: Encoding BCD
Select or Download Code
1+3 replies Replies are listed 'Oldest First'.
Re^2: Encoding BCD
by Aim9b (Monk) on Jun 13, 2007 at 16:06 UTC
    Corion, Thanks for the help, I especially like the debug piece at the end. My DecodeBCD is working (thanks in no small part to FunkyMonk), it's the ENcoding I'm having a problem with. getting from the 3 substrings, BACK to the \x12\x34..etc. Thanks.

      The following code works for me:

      use strict; use Test::More tests => 3; sub encode_BCD { return pack 'H*', join '', @_ }; is encode_BCD("123456"), "\x12\x34\x56"; is encode_BCD("00",3456),"\x00\x34\x56"; is encode_BCD("1",2),"\x12";
        I don't get it - aren't you just encoding it to Hex? That's not the same as BCD is it?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (20)
As of 2015-07-07 16:47 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 (91 votes), past polls