Hex File Crc 16 Calculator

Posted on

The three used in the on-line CRC calculation on this page are the 16 bit wide CRC16 and CRCCCITT and the 32 bits wide CRC32. The latter is probably most used now, because amongst others it is the CRC generator for all network traffic verification and validation. The test program can be used directly to test files or strings. You can also. Refreshing a checksum maintains the selected algorithms and selection parameters (entire document or a block within a document). The document must be open within the editor to refresh a checksum. Hex Workshop automatically replaces checksum results generated on a particular file to reduce confusion. Javascript CRC-16 Calculator. Here is a Javascript CRC-16 calculator I have written to demonstrate the different techniques that can be used to implement the CRC calculation in code. A CRC is calculated by dividing a generator polynomial into the message.

  1. Hex File Converter

Hex File Converter

Hi,
This is my first question on EE. I hope I have asked my question under the correct topics.
I have a problem with my CRC16 code, I think...
I have 2 senaiors:
1. My checksum byte 1 and 2 are swapped when compared to an excel CRC calculator I found online.. I tested this by entering different data and always found the same pattern.
2. Or my checksum is completely different to other online calculators.
Perhaps someone could point in the direction of a proven online CRC calulator.
For the life of me, I don't know which is correct or where my error could be.
My code:
Start value: 0xFFFF
Polynomial: 0xA001
4 bytes Hex Data: 0xACA5F69CCrc 16 algorithm
Checksum: 0x9E76
Would you mind helping me figure this out. Permalink

Join GitHub today

GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.

Sign up
Find file Copy path
brandonforsterdid the inputAsString fix correctly23d86cdNov 8, 2012
1 contributor
/*
* Brandon Forster
* CIS 3360 – Security in Computing Fall 2012
* Programming Assignment 2
* 7 November 2012
*/
packagecrc;
importjava.util.*;
importjava.io.*;
importjava.math.*;
publicclassCRCCalculator {
privatestaticFile userFile;
privatefinalstaticStringBINARY_POLYNOMIAL='10000010110001001';
publicstaticvoidmain(String[] args) {
//set up the keyboard scanner
Scanner stdin =newScanner(System.in);
String pathname;
//make the user input filenames until we find one we like
while (true)
{
System.out.println('Please enter path/ name for file.');
pathname= stdin.next();
File tempFile=newFile(pathname);
//check to make sure that the file the user entered exists
if (tempFile.exists() false)
{
System.out.println('Invalid pathname for file. Try again.');
continue;
}
else
{
//if it does, copy the object to the file of higher scope.
userFile= tempFile;
//verifyFile checks that the input is only hex characters.
//if it's not, it returns false and input loop begins again.
if (verifyFile(userFile) true)
break;
else
{
System.out.println('Input is not in hexadecimal format.');
continue;
}
}
}
//an infinite loop that has program functionality in it.
//will run until user tells it to exit.
int userChoice=0;
while (userChoice !=3){
//read in user input
System.out.println('-------------Menu-------------');
System.out.println('1: Calculate CRC');
System.out.println('2: Verify CRC');
System.out.println('3: Exit');
//make sure the user inputs an integer, like they're supposed to
try
{
userChoice = stdin.nextInt();
}
catch (InputMismatchException e)
{
//tell the user they dun goofed
System.out.println('Please enter a valid choice.n');
//eat the input from the scanner so we don't get stuck
stdin.next();
continue;
}
//the user wants to calculate CRC, go do that
if (userChoice 1)
calculateCRC();
//go verify CRC
elseif (userChoice 2)
verifyCRC();
//exit the program normally
elseif (userChoice 3)
System.exit(0);
//tell the user they entered something weird
else
{
System.out.println('Please enter a valid choice.n');
continue;
}
}
//we're done, close the scanner
stdin.close();
}
publicstaticvoidcalculateCRC()
{
printInit();
System.out.println('We will append sixteen zeros at the end of the binary input.n');
String inputString= hexToBinary(getInputAsString());
inputString = inputString +'0000000000000000';
System.out.println('The binary string answer at each XOR step of CRC calculation:');
printBinary(inputString);
String printString= inputString;
for (int i=0; i< inputString.length(); i++)
{
if (printString.charAt(i)'0')
continue;
if ((BINARY_POLYNOMIAL.length() + i +1 ) > printString.length())
{
printString= printString.substring(0, i)
+ xor(printString.substring(i), BINARY_POLYNOMIAL);
printBinary(printString);
break;
}
//what we've already done + what got xor'd
printString= printString.substring(0, i)
+ xor(printString.substring(i,(BINARY_POLYNOMIAL.length() +i)), BINARY_POLYNOMIAL)
+ inputString.substring((BINARY_POLYNOMIAL.length()+ i));
printBinary(printString);
}
//double reverse the string to get the checksum out
printString = reverse(printString);
printString = printString.substring(0, 16);
printString = reverse(printString);
System.out.print('Thus, the CRC is: (bin) ');
printBinary(printString);
System.out.println('which equals '+binaryToHex(printString)+' (hex)');
String input = getInputAsString();
System.out.println('Reading input file again: '+ input + binaryToHex(printString));
try{
BufferedWriter userFileWrite =newBufferedWriter(newFileWriter(userFile));
userFileWrite.write(input + binaryToHex(printString));
System.out.println('Closing input file.');
userFileWrite.close();
//this should never run ever.
} catch (IOException e) {
System.out.println('Something went wrong...');
}
return;
}
publicstaticvoidverifyCRC()
{
//save the input to a string variable for manipulation
String input = getInputAsString();
//print out the header
printInit();
if (input.length() <4)
{
System.out.println('Error: input too short. Terminating program.');
System.exit(1);
}
//do a double reverse to isolate the CRC
String crc= reverse(input);
crc= crc.substring(0, 4);
crc= reverse(crc);
//print out the CRC we isolated
System.out.print('The 16-bit CRC at the end of the file: (hex) '+ crc +'= ');
printBinary(hexToBinary(crc));
//print the chart
System.out.println('The binary string answer at each XOR step of CRC verification:');
//since we print after a permute, do a print of the input to get step 0
printBinary(hexToBinary(input));
//some variables that make our lives easier
String binaryInput = hexToBinary(input);
String printString = binaryInput;
//build the chart
for (int i=0; i< binaryInput.length() +1; i++)
{
//don't work on substrings that start with 0
if (printString.charAt(i)'0')
continue;
//if we're at the end and don't want to run into a StringOutOfBounds
if ((BINARY_POLYNOMIAL.length() + i) > printString.length())
{
//string we've processed thus far + xor processed
printString= printString.substring(0, i)
+ xor(printString.substring(i), BINARY_POLYNOMIAL);
printBinary(printString);
//aaaaand we're done!
break;
}
//string we've processed thus far + xor processed
//+ whatever is past that that we haven't gotten to yet in the string
printString= printString.substring(0, i)
+ xor(printString.substring(i,(BINARY_POLYNOMIAL.length() +i)), BINARY_POLYNOMIAL)
+ binaryInput.substring((BINARY_POLYNOMIAL.length()+ i));
printBinary(printString);
}
boolean crcCheckPass =true;
//if there is a 1 anywhere in the final string, the check did not pass.
for (int i=0; i < printString.length(); i++)
{
if (printString.charAt(i) '1')
crcCheckPass =false;
}
//GET RESULTS.
System.out.print('nDid the CRC check pass? (Yes or No): ');
if (crcCheckPass true)
System.out.println('Yes');
else
System.out.println('No');
}
publicstaticvoidprintInit()
{
//print out the input file
System.out.println('The input file (hex): '+
getInputAsString());
//print out a binary representation of the input hex
System.out.println('The input file (bin): ');
printBinary(hexToBinary(getInputAsString()));
System.out.println('');
//print out polynomial used
System.out.print('The polynomial that was used '+
'(binary bit string): ');
printBinary(BINARY_POLYNOMIAL);
System.out.println('');
}
//return a string representation of input
publicstaticStringgetInputAsString()
{
String inputString='';
try {
Scanner scn =newScanner(userFile);
//while the scanner can find strings in the input.
while (scn.hasNext())
inputString = inputString + scn.next();
scn.close();
//this should never run ever.
} catch (FileNotFoundException e) {
System.out.println('Something went wrong...');
}
return inputString;
}
//make sure input is well formed.
publicstaticbooleanverifyFile(Fileinput)
{
try {
Scanner hexScanner =newScanner(input);
//changes the delimiter to the empty string so that .next()
//returns one character at a time.
hexScanner.useDelimiter('');
while (hexScanner.hasNext() true)
{
//converts the read in string to a char, then checks if it is
//in valid ascii ranges
char check = hexScanner.next().toUpperCase().toCharArray()[0];
if (check <'0')
{
hexScanner.close();
returnfalse;
}
elseif (check >'F')
{
hexScanner.close();
returnfalse;
}
elseif (check >'9'&& check <'A')
{
hexScanner.close();
returnfalse;
}
}
hexScanner.close();
//if it passed above checks, it must be okay.
returntrue;
//this should never run ever.
} catch (FileNotFoundException e) {
System.out.println('Something went wrong...');
returnfalse;
}
}
//convert hexadecimal to binary using BigInt
publicstaticStringhexToBinary(StringhexNumber)
{
BigInteger temp =newBigInteger(hexNumber, 16);
return temp.toString(2);
}
//print binary numbers using the defined rules
publicstaticvoidprintBinary(StringbinaryNumber)
{
//uses regexes that I looked up how to do online to perform
//required ops.
binaryNumber= binaryNumber.replaceAll('.{32}', '$0n');
//does a double reverse to get proper formatting on bits
binaryNumber= reverse(binaryNumber);
binaryNumber= binaryNumber.replaceAll('.{4}', '$0 ');
binaryNumber= reverse(binaryNumber);
System.out.print(binaryNumber);
//some functionality that ensures there's always a newline
if (binaryNumber.length() <32)
System.out.println('');
}
//reverses a string recursively
publicstaticStringreverse(Stringstr) {
if (str.length() <=1) {
return str;
}
return reverse(str.substring(1, str.length())) + str.charAt(0);
}
//converts a binary string to hexadecimal
publicstaticStringbinaryToHex (StringbinaryNumber)
{
BigInteger temp =newBigInteger(binaryNumber, 2);
return temp.toString(16).toUpperCase();
}
//performs an exclusive or on two binary strings
publicstaticStringxor (Stringone, Stringtwo)
{
//operate based on the smallest string
int minLength=Math.min(one.length(), two.length());
String output='';
//for each character in the string, do a bitwise XOR
for (int i=0; i< minLength; i++)
output= output + (one.charAt(i) ^ two.charAt(i));
return output;
}
}
  • Copy lines
  • Copy permalink