Skip to content

Instantly share code, notes, and snippets.

@scottmkroberts
Created February 29, 2012 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmkroberts/1943979 to your computer and use it in GitHub Desktop.
Save scottmkroberts/1943979 to your computer and use it in GitHub Desktop.
CRC-16 Hash Computation for Case Labels
console.log("hello CRC16");
function CreateCrc16(){
var polynomial,value,temp;
var table=new Array(256);
polynomial=40961;
for(i=0;i<table.length;++i){
value=0;temp=i;
for(j=0;j<8;++j){if(0!=((value^temp)&1)){
value=(value>>1)^polynomial
}else{
value>>=1
}
temp>>=1
}
table[i]=value
}return table
}
var crc,table,testString,index;
var GTIN = "10850510002011";
var Lot = "46587443HG234"
var date = ""
testString = "1088100603702601227131123456120112";
console.log("Plain Text = " +testString);
/*if(testString != ""){
testString=GTIN+Lot+date;
}*/
crc=0;
table=CreateCrc16();
for(i=0;i<testString.length;++i){
index=(crc^testString.charCodeAt(i))%256;
crc=(crc>>8)^table[index]
}
console.log("crc = " +crc);
var vc=(crc%10000)+"";
var length=vc.length;
for(i=vc.length;i<4;i++)
{
vc="0"+vc
}
console.log("Small Digits" + vc.substr(0,2));
console.log("Large Digits" + vc.substr(2,2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment