Oh highly esteemed monks, hear my plea for knowledge.
I am attempting to duplicate in Perl the checksum functionality in the following C code.
#include <stdio.h> /********************************************************************* +***/ /* checksum -- verify checksum */ /********************************************************************* +***/ int checksum (buff, bufflen) char *buff ; int bufflen ; { int ctr ; int retval ; char tmpstr [5] ; long strtol () ; unsigned int chk_sum = (unsigned) 0 ; int twos_comp ; ctr = 0 ; while (ctr < (bufflen - 5)) { chk_sum = chk_sum + (buff [ctr] & 0x7f) ; ctr ++ ; } strncpy (tmpstr, &buff [bufflen - 5], 4) ; tmpstr [4] = '\0' ; twos_comp = (int) strtol (tmpstr, (char **) NULL, 16) ; retval = (((chk_sum + twos_comp) & 0xFFFF) == 0) ? 0 : 1 ; return (retval) ; }
Problem? I know ZIP about C, and the definitions I have found for the various functions (all of which appear to be "standard" C ) don't give me enough understanding of what the C function actually does to allow me to replicate the functionality in Perl.
I have gotten as far as the second to last couple of lines, and there I am completely stumped.

Sample input consists of <SOH>9999FF1B<ETX> where <SOH>=0x01 and <ETX>=0x3 respectively.
The inbound is an ASCII string.
Output is either a 0 or 1 (failure / success). I seek either of two things.
1. A plain english description of what the second to the last line of code is actually doing, step by step.
2. An example in Perl of what it is doing. I am quite willing to beat my head against the example until I garner understanding of what is actually happening.

A humble(d) acolyte,
Mike Gucciard

In reply to 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.