Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2007, 2:52 PM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Implementation of CRC32

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. Now irrelevant of the actual language, this actionscript code doesn't use any fancy Flash stuff other than the function trace() which outputs results in a console-like fashion for debugging. 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.

Here's an explanation on crc32

The actual value I'm trying to achieve is 0xe8b7be43
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 1st, 2007, 11:41 PM   #2
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Question

As per this page I just discovered that you can append the crc32 checksum to the original message and that if you compute the crc32 value of that then the result should be 0. I did this with my own program 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
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Old Mar 3rd, 2007, 2:13 PM   #3
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 431
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
I changed the script a bit and now it will actually verify the checksum by appending it to the original message and computing the remainder.
trace('//////////////////////');
trace('// Compute CheckSum //');
trace('//////////////////////');
var mess:String = 'a';
var myArray:Array = new Array(mess);
myArray.push(crc32(myArray));
trace('/////////////////////');
trace('// Verify CheckSum //');
trace('/////////////////////');
crc32(myArray);

function crc32(input:Array):String {
   var hexInput:String = '';
   for(var i:Number = 0; i < input[0].length; i++) {
      hexInput += dechex(input[0].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;
      }
   }
   var iterations:Number = binaryInput.length;
   var binaryGenerator:String = '100000100110000010001110110110111'; //Big-endian
   switch(input.length){
      case 1:
      for(var i:Number = 0; i < binaryGenerator.length - 1; i++){
         binaryInput += '0';
      }
      break;
      case 2:
      binaryInput += input[1];
      break;
      default:
      return 'Error';
      break;
   }
   for(var i:Number = 0; i < iterations; i++) {
      trace('  ' + binaryInput);
      hexInput = '';
      for(var j:Number = 0; j < i; j++){
         hexInput += ' ';
      }
      if(binaryInput.charAt(i) == '1') {
         trace('- ' + hexInput + binaryGenerator);
      }else{
         for(var j:Number = 0; j < binaryGenerator.length; j++){
            hexInput += '0';
         }
         trace('- ' + hexInput);
      }
      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');
            }
         }
      }
      hexInput = '';
      for(var j:Number = 0; j < binaryInput.length; j++){
         hexInput += '-';
      }
      trace('  ' + hexInput);
   }
   trace('  ' + binaryInput);
   return binaryInput.substr(binaryInput.length - (binaryGenerator.length - 1));
}

function replace(original:String, index:Number, replacement:String):String {
   if(index == 0){
      return replacement + 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;
}
The output is:
//////////////////////
// Compute CheckSum //
//////////////////////
  0110000100000000000000000000000000000000
- 000000000000000000000000000000000
  ----------------------------------------
  0110000100000000000000000000000000000000
-  100000100110000010001110110110111
  ----------------------------------------
  0010000000110000010001110110110111000000
-   100000100110000010001110110110111
  ----------------------------------------
  0000000010101000011001001101101100100000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-     000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-       000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
-        000000000000000000000000000000000
  ----------------------------------------
  0000000010101000011001001101101100100000
/////////////////////
// Verify CheckSum //
/////////////////////
  0110000110101000011001001101101100100000
- 000000000000000000000000000000000
  ----------------------------------------
  0110000110101000011001001101101100100000
-  100000100110000010001110110110111
  ----------------------------------------
  0010000010011000001000111011011011100000
-   100000100110000010001110110110111
  ----------------------------------------
  0000000000000000000000000000000000000000
-    000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-     000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-      000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-       000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
-        000000000000000000000000000000000
  ----------------------------------------
  0000000000000000000000000000000000000000
Why am I right? And yet SO wrong?
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with node implementation brad sue C++ 12 Mar 4th, 2007 11:27 PM
Perl-type Regular Expression Implementation SittingDuck Other Programming Languages 4 Dec 7th, 2005 11:30 AM
implementation of cpuid and graph in my benchmark programm chorijan C++ 0 Sep 7th, 2005 6:27 AM
queue implementation problems epswing C 6 Apr 20th, 2005 3:44 PM
Help with stack implementation ridley C++ 0 Feb 26th, 2005 2:26 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC