Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CRC algorithm

 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards,
Sen
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see the CRC32 class in java.util.zip?
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc,

Thanks for the reply. I might be wrong but I trust that there is only one and only one CRC32 algorithm, right?

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.

Regards,
Sen
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Sen
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code snippet with the crc32 calls. I get the input string using a helper class.

public class crc32 {

public static final String initvalue = "37a52d19"; // hex string

public static void main (String args[]) {

String inputstring = "";
String checkstring = "";
String resultstring = "";

byte[] crcinput ;
byte[] initvalarr;

long result = 0;

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);

crcinput = checkstring.getBytes();
initvalarr = initvalue.getBytes();
leng = checkstring.length();


// Compute CRC checksum.

CRC32 crc32 = new CRC32();
crc32.reset();

// Update initial value first.

crc32.update(initvalarr,0,initvalarr.length);
crc32.update(crcinput,0,crcinput.length);

result = crc32.getValue();
resultstring = Long.toHexString(result);

System.out.println(" Validation result: " + resultstring);
}
}

Please advise.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sen George:
Please advise.

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?
 
Sen George
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

http://www.zorc.breitbandkatze.de/crc.html

Click on CRC-32 button.
Give an initial value of 37a52d19.
Give the data sequence of 0C039C32610
Uncheck the boxes 'reverse data bytes' and 'reverse CRC result before final XOR'
Hit Compute button.

The result will be 162CC0F9.

This result conflicts with the output of my program.

Please advise.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic