473,326 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

c to java CRC32 code conversion

Hi,

I am attempting to convert the following code written in c to
equivalent java code. This is the CRC32 algorithm used by a GPS
received I am interfacing with. Unfortunately, the CRC32 class
provided in the java API does not suit my needs because it does not
allow manipulation of the polynomial being used. Below is the original
c-code followed by my attempt to convert the code to java.
Unfortunately, my code does not produce the correct results. I assume
I am missing something in regards to the data types I am using,
specifically the change from unsigned long in c to a java int, or
something in the bit-shift operations. Any insight you can provide
would be helpful. Thanks.

c-code********************************

#define CRC32_POLYNOMIAL 0xEDB88320L
/*
--------------------------------------------------------------------------
Calculate a CRC value to be used by CRC calculation functions.
--------------------------------------------------------------------------
*/
unsigned long CRC32Value(int i)
{
int j;
unsigned long ulCRC;
ulCRC = i;
for ( j = 8 ; j > 0; j-- )
{
if ( ulCRC & 1 )
ulCRC = ( ulCRC >> 1 ) ^ CRC32_POLYNOMIAL;
else
ulCRC >>= 1;
}
return ulCRC;
}

/*
--------------------------------------------------------------------------
Calculates the CRC-32 of a block of data all at once
--------------------------------------------------------------------------
*/
unsigned long CalculateBlockCRC32(
unsigned long ulCount, /* Number of bytes in the data block */
unsigned char *ucBuffer ) /* Data block */
{
unsigned long ulTemp1;
unsigned long ulTemp2;
unsigned long ulCRC = 0;
while ( ulCount-- != 0 )
{
ulTemp1 = ( ulCRC >> 8 ) & 0x00FFFFFFL;
ulTemp2 = CRC32Value( ((int) ulCRC ^ *ucBuffer++ ) & 0xff );
ulCRC = ulTemp1 ^ ulTemp2;
}
return( ulCRC );
}
java code ************************************************

private static final int CRC32_POLYNOMIAL = 0xEDB88320;
/*
--------------------------------------------------------------------------
Calculate a CRC value to be used by CRC calculation functions.
--------------------------------------------------------------------------
*/
private static int CRC32Value(int i)
{
short j;
int ulCRC;
ulCRC = i;

for (j = 8; j > 0; j--) {
if ((ulCRC & 1) == 1)
ulCRC = (ulCRC >>> 1) ^ CRC32_POLYNOMIAL;
else
ulCRC >>>= 1;
}
return ulCRC;
}

/*
--------------------------------------------------------------------------
Calculates the CRC-32 of a block of data all at once
--------------------------------------------------------------------------
*/
public static int calculateCRC32(int length, byte[] buffer)
{
int ulTemp1;
int ulTemp2;
int ulCRC = 0;

for(int i = 0; i < length; i++) {
ulTemp1 = ( ulCRC >>> 8 ) & 0x00FFFFFF;
ulTemp2 = CRC32Value( ((int) ulCRC ^ buffer[i] ) & 0xff );
ulCRC = ulTemp1 ^ ulTemp2;
}
return ulCRC;
}

Jul 17 '05 #1
1 9973

Looks like it works to me.

I built the following test case based on the output of a simple C
driver program. A test case necessary to determine what we are
talking about here.

import junit.framework.TestCase;

public class TestCRC32 extends TestCase {
public void test1() {
CRC32 t1 = new CRC32();
byte[] buf = new byte[20];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (i * 511);
}
assertEquals(0x9d335772, t1.calculateCRC32(buf));
t1.reset();
t1.calculateCRC32(buf, 0, 10);
assertEquals(0x9d335772, t1.calculateCRC32(buf, 10, 10));
}
}

This code computes the crc in a single pass and in an incremental
fashion to show off some increased flexibility in the new code.

I also adapted your code slightly so that you can create a CRC object
that remembers its state and so that the polynomial can be reset. I
also modified it to match normal Java coding standard a bit closer.
Hungarian notation is really just an affectation in Java; it was
necessary in assembler and in poorly typed environments such as the
win32 API, but is not useful in Java. Also, it is helpful if you
follow Javadoc style.
/**
* Implements a general CRC class that lets you change the polynomial.
*/
public class CRC32 {
private int polynomial = 0xEDB88320;
private int crc = 0;

/**
* Calculates a CRC value for a byte to be used by CRC calculation
functions.
*/
private int CRC32Value(int i) {
int crc = i;

for (int j = 8; j > 0; j--) {
if ((crc & 1) == 1)
crc = (crc >>> 1) ^ polynomial;
else
crc >>>= 1;
}
return crc;

}

/**
* Calculates the CRC-32 of a block of data all at once
*/
public int calculateCRC32(byte[] buffer, int offset, int length) {
for (int i = offset; i < offset + length; i++) {
int tmp1 = (crc >>> 8) & 0x00FFFFFF;
int tmp2 = CRC32Value(((int) crc ^ buffer[i]) & 0xff);
crc = tmp1 ^ tmp2;
}
return crc;
}

/**
* Calculates the CRC-32 of a block of data all at once
*/
public int calculateCRC32(byte[] buffer) {
return calculateCRC32(buffer, 0, buffer.length);
}

/**
* Resets the state to process more data.
*/
public void reset() {
crc = 0;
}

public void setPolynomial(int polynomial) {
this.polynomial = polynomial;
}
}

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ricky Romaya | last post by:
Hi, I'm working on a file upload script. I need to calculate the CRC32 of the file(s) which are successfully uploaded. How can I do this? PHP only have CRC32 function for strings. However, the...
5
by: Roy Schestowitz | last post by:
Hi, I have asked this question in a C++ newsgroup, but I did not get quite the answer that I had hoped for. I would like to find out if there are tools which make large transitions from C++ to...
2
by: nobody | last post by:
1) Does anyone know if the CRC32 algorithm in binascii has a name? There seem to be a TON of different CRC32 methods; different polynomials, different byte orders, different seeds, some flip the...
6
by: Weiguang Shi | last post by:
Hi there, I'm thinking of using binascii.crc32 as a hash-function when I read in the reference http://www.python.org/doc/current/lib/module-binascii.html: crc32( data) Compute CRC-32, the...
0
by: Christophe Elek | last post by:
Ok, I am completly at lost :) in both cases (my Yenc and zip utilities) i try to do the following 1) get a String 2) transform in byte 3) so some computation (either adding value to the byte...
14
by: Don | last post by:
Hi NG. Does anyone know of a place where I could download/get a C implementation of a CRC32 check. I would like a simple function that, for example, had a pointer to where the data to be CRC32...
9
by: UnixUser | last post by:
I am looking for some source code to run on Linux that will enable me to calculate and return a CRC32 value from a string of text. I have found one from snippets.org, but I cannot get it to...
7
by: jeff | last post by:
We have a library written in Java that we need to port to .NET (and we need to maintain both versions of the library). I've done some preliminary research on approaches to this but none of them...
12
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.