# [web] Trouble with a crc32 implementation

This topic is 4133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok now I realize this isn't the best or most efficient or even a way that I should implement CRC32, BUT I figure if I can do it this way first then the other better ways will be easier to understand (or at the very least please afford me that discretion). I'm basically trying to compute the crc32 hash of the letter 'a' using ActionScript. So here's the code:
trace(crc32('a'));

function crc32(input:String):String {
var hexInput:String = '';
for(var i:Number = 0; i < input.length; i++) {
hexInput += dechex(input.charCodeAt(i));
}
var binaryInput:String = '';
for(var i:Number = 0; i < hexInput.length; i++) {
switch(hexInput.charCodeAt(i)) {
case 48:
binaryInput += '0000';
break;
case 49:
binaryInput += '0001';
break;
case 50:
binaryInput += '0010';
break;
case 51:
binaryInput += '0011';
break;
case 52:
binaryInput += '0100';
break;
case 53:
binaryInput += '0101';
break;
case 54:
binaryInput += '0110';
break;
case 55:
binaryInput += '0111';
break;
case 56:
binaryInput += '1000';
break;
case 57:
binaryInput += '1001';
break;
case 97:
binaryInput += '1010';
break;
case 98:
binaryInput += '1011';
break;
case 99:
binaryInput += '1100';
break;
case 100:
binaryInput += '1101';
break;
case 101:
binaryInput += '1110';
break;
case 102:
binaryInput += '1111';
break;
}
}
binaryInput += '00000000000000000000000000000000';
var binaryGenerator:String = '100000100110000010001110110110111';
for(var i:Number = 0; i < binaryInput.length - binaryGenerator.length; i++) {
if(binaryInput.charAt(i) == '1') {
for(var j:Number = 0; j < binaryGenerator.length; j++) {
if(binaryInput.charAt(i + j) != binaryGenerator.charAt(j)){
binaryInput = replace(binaryInput, i + j, '1');
}else{
binaryInput = replace(binaryInput, i + j, '0');
}
}
}
}
return binaryInput.substr(binaryInput.length - (binaryGenerator.length - 1));
}

function replace(original:String, index:Number, replacement:String):String {
if(index == 0){
return replacement.concat(original.substr(1));
}
if(index == original.length - 1){
return original.substr(0, original.length - 1) + replacement;
}
return original.substr(0, index) + replacement + original.substr(index + 1);
}

function dechex(val:Number):String {
if(val < 0){
val += Math.pow(2, 32);
}
var output:String = '';
for(var i:Number = 0; i < 8; i++){
var remainder:Number = val % 16;
val = (val - remainder) / 16;
switch(remainder){
case 10:
output = 'a' + output;
break;
case 11:
output = 'b' + output;
break;
case 12:
output = 'c' + output;
break;
case 13:
output = 'd' + output;
break;
case 14:
output = 'e' + output;
break;
case 15:
output = 'f' + output;
break;
default:
output = remainder + output;
break;
}
}
do{
if(output.charCodeAt(0) == 48){
output = output.substr(1, output.length - 1);
}
}while(output.charCodeAt(0) == 48);
return output;
}
This results in 1010 1000 0110 0100 1101 1011 0010 0000. Now I performed this calculation by hand and that's the actual result of the calculation I performed by hand. SO somewhere along the line I'm missing something I just don't know what. It may have something to do with big-endian or little-endian format. I'm hoping someone can point me in the right direction in terms of what I'm doing wrong. The actual value I'm trying to achieve is 0xe8b7be43

##### Share on other sites
I just discovered that you can append the computed crc32 checksum to the original message and that if you compute the remainder of that value then the result should be 0. I did this by modifying my own program a bit and sure enough the result was 0. Soooooooo now I'm even more confused 'cause it seemingly works correctly. What am I doing wrong? This is so discouraging

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631426
• Total Posts
3000019
×