Created
February 29, 2012 19:57
-
-
Save scottmkroberts/1943979 to your computer and use it in GitHub Desktop.
CRC-16 Hash Computation for Case Labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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