I needed to duplicate the Android CRC32 exactly in a separate desktop
C application, but the code that I found on-line did not exactly agree
with the Android version.  So I did some trial and error tests and
developed the following C code which does agree with the Android CRC32
(assuming a 32-bit unsigned int in the desktop application):

unsigned int accum=0;   //..equivalent to a CRC32.reset()

CRC32.update(byteValue) in Java is equivalent to:

accum ^= byteValue;
for(int i=0; i<8; i++)
{
  unsigned int b = 0;
  if(accum & 1)   b = 0xedb88320;
  accum =((accum>>1) & 0x7fffffff) ^ b;
}
accum ^= 0xd202ef8d;

At any time, accum is the CRC.getValue().  (You may be able to avoid
the "& 0x7fffffff" if you are sure your compiler does a real logical
right shift and not an arithmetic one.)

As with any such bit-wise CRC implementation, it can be speeded up by
using a pre-calculated table of 256 32-bit ints.  That table can be
derived using the above bit-wise algorithm at design-time.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to