wood burning stoves 2.0*
The moose likes Java in General and the fly likes CRC calculations and endian'ness Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "CRC calculations and endian Watch "CRC calculations and endian New topic

CRC calculations and endian'ness

Aaron Roberts
Ranch Hand

Joined: Sep 10, 2002
Posts: 174
I've been working on an app which parses files and interprets the data as littleEndian format. If the file contains a CRC in it, I can convert the bytes to bigEndian format for use in java, but what about the CRC routine itself? Does a CRC routine result depend upon the endian'ness of the machine its running on? I'm assuming the CRC routine to be fed with byte values.
don cline
Ranch Hand

Joined: Oct 10, 2003
Posts: 35
The byte order of a file does effect the CRC. I've been using CRC32.
The following is a program that calculates the CRC32 from a file, try it out yourself:
import java.util.*;
import java.io.*;
import java.util.zip.*;
public class CalculateChecksumFromFile
static public void main(String[] args)
String fileContents = "";

if (args.length < 1)
System.out.println("Use: java CalculateChecksumFromFile <Filename>");
System.out.println("Calculating checksum for:"+args[0]);

File currentFile = new File(args[0]);
FileReader fR;
fR = new FileReader(currentFile);
char[] buff = new char[16*1048576];
int readChars = fR.read(buff);
while (readChars > 0)
fileContents = fileContents + (new String(buff,0,readChars));
catch (IOException iOE)
byte[] byteArray = (fileContents).getBytes();
byte[] buffer = new byte[8192];
ByteArrayInputStream bAIS = new ByteArrayInputStream(byteArray);
CRC32 cRC32 = new CRC32();
CheckedInputStream cIS = new CheckedInputStream(bAIS, cRC32);
int byteRead = 0;
while (byteRead != -1) byteRead = cIS.read(buffer, 0, buffer.length);
catch (IOException iOE)
String calcChecksum = Long.toHexString(cIS.getChecksum().getValue());
System.out.println("Checksum is :"+calcChecksum);
I agree. Here's the link: http://aspose.com/file-tools
subject: CRC calculations and endian'ness