in reply to Conversion of C code to Perl-ese.

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.

Replies are listed 'Best First'.
Re: Re: Conversion of C code to Perl-ese.
by gooch (Monk) on Aug 14, 2003 at 15:52 UTC
    Thank you all so very much for your efforts.
    I especially want to thank CombatSquirrel, and BrowserUK.
    Between the two of you I now not only understand more clearly what the C code does, but how it is doing it, and +++ to BrowserUK, I hadn't even gotten to the point of thinking that having a routine to encode with might be handy (For testing, say). That was a completely unexpected and much appreciated bonus.

    I am formally out of the doldrums, and sailing full speed ahead on my project.

    It is my hope that someday I will accumulate enough knowledge to be able to contribute in similar fashion, here in the Monestary, to the enlightenment of my fellow Monks.

    A much less confused Acolyte,
    Mike Gucciard