Try this version.

#! perl -slw use strict; sub checksum { my( $string ) = @_; my $chk_sum = 0; $chk_sum += $_ for unpack 'C*', substr $string, 0, -5; my $twos_comp = hex( substr $string, -5, 4 ); return ( ( $chk_sum + $twos_comp ) & 0xFFFF ) ? 1 : 0; } print "$_ : ", checksum $_ for "\x019999FF1B\x03", "\x019998FF1B\x03"; __END__ P:\test>283701 ?9999FF1B? : 0 ?9998FF1B? : 1

As for what is going on. The encoding routine adds up the (7-bit) ascii values of the message, takes the 16-bit twos complement of the total and adds the result to the end of the message.

eg.

9999 = 57 + 57 + 57 + 57 = 228. ~ 228 = -229 decimal = FF1B hex (16-bit) Add the header and trailer characters = <SOH>9999FF1B<ETX>

To check the transmission was uncorrupted, the checksum routine totals up the (7-bit) ascii values of the message - the last 5 characters.

1 + 57 + 57 + 57 + 57 = 229

Converts the last 5-1 characters back from hex

FF1B = 65307

Then adds the two together (discarding any bits greater than 16 which could happen with longer messages)

(65307 + 229) = 65536 65536 & 65535 = 0x10000 & 0xFFFF = 0

If the result is 0, the checksum matched and the function returns 0 to indicate success--or perhaps a lack of failure:)

A perl implementation of the encoding routine might look like this

sub build_string { my( $string ) = @_; my $chksum = 0; $chksum += $_ for map{ $_ & 0x7F } unpack 'C*', $string; $chksum = (~$chksum & 0xFFFF); return chr(1) . $string . sprintf( '%4x', $chksum ) . chr(3); }

HTH.

If the result isn't 0, then the checksum didn't match and the function return 1--to indicate corruption occured?

Note: The return value is backwards from your expectation, and mine, but that is what the code is doing (unless I am completely misinterpreting it, which is always possible!).


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.


In reply to Re: Conversion of C code to Perl-ese. by BrowserUk
in thread Conversion of C code to Perl-ese. by gooch

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.