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




[PHP] Re: fletcher's checksum

2003-01-09 Thread Dave Gervais
Thanks for the reply.

I do have the C code that does this. If anybody can easily translate it 
to PHP, it would be very apriciated.

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.

Thanks again,

Dave Gervais



Ford, Mike [LSS] wrote:

-Original Message-
From: Dave Gervais [mailto:[EMAIL PROTECTED]]
Sent: 08 January 2003 19:11

Does anybody know how to implement the 8 and 16 bit versions of 
Fletcher's checksum using PHP? I hit google and came up dry. It's 
easy enough to find how to do it in C, but PHP if a different story.
  


Well, I have no idea what Fletcher's checksum is, but if you have a 
version
in C that's not too long (20 lines of code, say), it shouldn't be too
difficult to convert it into PHP.  There should be enough people here
sufficiently proficient in both C and PHP to at least give you a 
pointer in
the right direction!

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