Has anyone implemented a program to compute CRC32 algorithm either as a standalone Java program or by means of a wrapper invoking the standard Java API? If so, would you be kind enough to give some guidelines to implement the same.
I looked through the API and it was a bit confusing.
Originally posted by Sen George: Please advise whether the checksum returned by the call CRC32.getValue() is always a constant or it would vary depending on the length of the input string.
Have you considered that it might be faster to write the 10 lines of code that would answer this question for you?
Joined: Sep 21, 2005
In my earlier reply, I didn't mean a constant output. I meant an output of constant length. Apologize for the same. Anyway, I ran the test for a few different strings and the length of the output always varies.
Here is what confuses me. I found a CRC converter on the web (http://www.zorc.breitbandkatze.de/crc.html) and the tool always seems to return a 8-character hex string. I am surprised that the results between the free converter tool and the standard Java API call are different.
That's why I logged the thread to see if anyone has used this particular API.
The CRC32 algorithm gives you a 32-bit checksum as the result of the algorithm, so the result is always the same length - it's a 32-bit integer.
Note that the method CRC32.getValue() returns a long, which is 64 bit in Java. Only the lower 32 bits of this long are used in the CRC32 class. The reason that it returns a 64-bit long is that the metod getValue() is inherited from the interface java.util.zip.Checksum, which is a generic interface for checksum algorithms.
The 8-character hex string you get from the other tool you tried is just a string representation of the 32-bit checksum. The checksum is just a number, and it's up to you how you convert it to a string (as an 8-char hex string or something else).
How do the results of that tool and using the Java API method differ?
Are you using the CRC32 class correctly? To compute the checksum using CRC32, you first create an instance of CRC32, then you give it the data you want to compute the checksum over by calling one of the update(...) methods one or more times, and when that's done you call getValue() to get the checksum value. [ August 15, 2006: Message edited by: Jesper Young ]
inputHelper inp = new inputHelper(); System.out.println(" ****** CRC program ****** "); System.out.println(); inputstring = inp.getUserInput(" Please enter a string to be checked: "); checkstring = inputstring.substring(0,11);
So what are you confused about? You were comparing the output of this program to the output of a tool you are using, how does the result differ? Is the input to both programs exactly the same?
What is the initvalue string in your program and why are you using it in computing the checksum? Note that if somebody enters the string "abcdefghijk", your Java program is calculating the checksum over the bytes that represent the string "37a52d19abcdefghijk". Is that what you wanted?
Joined: Sep 21, 2005
Thanks for the response. Let me clarify the situation a little bit. To begin with, we have an existing C program which does the CRC32 computation-this uses an initial value. I cross-checked the results by going to the following web site and performing the steps listed below.
I'm not an expert on CRC algorithms. On that website you can choose different CRC polynomials. I don't know which one the Java class java.util.zip.CRC32 uses.
The CRC32 class doesn't seem to have a method that you can use to specify the initial value, and the API documentation doesn't say what the initial value is.
Anyway, what you're trying to do is putting in the string "37a52d19". Do you know what initvalue.getBytes() returns? It returns the following array of bytes (hexadecimal):
33 37 61 35 32 64 31 39
You are calculating the checksum over these bytes. That's not the same as setting the initial value to the number 0x37a52d19.
I wrote a small test program:
When I run this, the result is: 3610A686
When I go to the website http://www.zorc.breitbandkatze.de/crc.html and I leave everything on the default values, and I enter "hello" in the "Data sequence" text box and click "compute!", I get the same result (3610A686) in the "Result" text box.