RE: [PHP] Re: fletcher's checksum

2003-01-10 Thread Ford, Mike [LSS]
 -Original Message-
 From: Dave Gervais [mailto:[EMAIL PROTECTED]]
 Sent: 09 January 2003 21:36
 
 Here is the C code. There is a decode function, but I don't 
 need it in 
 PHP because I have a C program listening to serial port on 
 the other end 
 that will validate the checksum.
 
 
 /*
 * operator fletcher_encode
 */
 fletcher_encode( buffer, count )
 unsigned char* buffer;
 long count;
 {
   int i;
   unsigned char c0 = 0;
   unsigned char c1 = 0;
   * ( buffer + count - 1 ) = 0;
   * ( buffer + count - 2 ) = 0;
   for( i = 0; i  count; i++)
   {
  c0 = c0 + * ( buffer + i );
  c1 =c1 +c0;
   }
   * ( buffer + count - 2 ) = c0 - c1;
   * ( buffer + count - 1 ) = c1 - 2*c0;
 }
 
 
 My problem with PHP was with the unsigned char.

Well, as I guess that would effectively be an integer in the range 0-255, I'd just 
treat it as an integer and reduce it modulo 256 in places where it might overflow that 
value.

Exactly how this translates into your PHP code depends on how you're translating the 
rest of the routine, and especially what you turn buffer into, but the loop might go 
something like:

   for ($i=0; $i$count; $i++):
  $c0 = ($c0+$buffer[i])%256;
  $c1 = ($c1+$c0)%256;
   endfor;

You could also do the modulo 256 reduction by doing a bitwise and with 0xff (or 0377, 
or 255), of course -- this is likely to be more efficient, and may, depending on your 
point of view, be more obvious as to what's going on.  Then your loop might look like 
this:

   for ($i=0; $i$count; $i++):
  $c0 = ($c0+$buffer[i])0xff;
  $c1 = ($c1+$c0)0xff;
   endfor;

Hope this is helpful and sets you off on the right track.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: fletcher's checksum

2003-01-10 Thread Scott Fletcher
For a moment, I thought you were referring to me when you said Fletcher
since it's my name also.  :-)

FletchSOD
Mike Ford [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
  -Original Message-
  From: Dave Gervais [mailto:[EMAIL PROTECTED]]
  Sent: 09 January 2003 21:36
 
  Here is the C code. There is a decode function, but I don't
  need it in
  PHP because I have a C program listening to serial port on
  the other end
  that will validate the checksum.
 
 
  /*
  * operator fletcher_encode
  */
  fletcher_encode( buffer, count )
  unsigned char* buffer;
  long count;
  {
int i;
unsigned char c0 = 0;
unsigned char c1 = 0;
* ( buffer + count - 1 ) = 0;
* ( buffer + count - 2 ) = 0;
for( i = 0; i  count; i++)
{
   c0 = c0 + * ( buffer + i );
   c1 =c1 +c0;
}
* ( buffer + count - 2 ) = c0 - c1;
* ( buffer + count - 1 ) = c1 - 2*c0;
  }
 
 
  My problem with PHP was with the unsigned char.

 Well, as I guess that would effectively be an integer in the range 0-255,
I'd just treat it as an integer and reduce it modulo 256 in places where it
might overflow that value.

 Exactly how this translates into your PHP code depends on how you're
translating the rest of the routine, and especially what you turn buffer
into, but the loop might go something like:

for ($i=0; $i$count; $i++):
   $c0 = ($c0+$buffer[i])%256;
   $c1 = ($c1+$c0)%256;
endfor;

 You could also do the modulo 256 reduction by doing a bitwise and with
0xff (or 0377, or 255), of course -- this is likely to be more efficient,
and may, depending on your point of view, be more obvious as to what's going
on.  Then your loop might look like this:

for ($i=0; $i$count; $i++):
   $c0 = ($c0+$buffer[i])0xff;
   $c1 = ($c1+$c0)0xff;
endfor;

 Hope this is helpful and sets you off on the right track.

 Cheers!

 Mike

 -
 Mike Ford,  Electronic Information Services Adviser,
 Learning Support Services, Learning  Information Services,
 JG125, James Graham Building, Leeds Metropolitan University,
 Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
 Email: [EMAIL PROTECTED]
 Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php