CRC sometimes returns negative value #21
	
		Labels
		
	
	
	
	
	
	
		No Milestone
		
			
		
	
	
		
		
		
			No project
			
				
			
		
	
	
	
	
	
		No Assignees
		
			
		
	
	
	
		1 Participants
		
	
	
		
		
			Notifications
			
				
			
		
	
	
	
	Due Date
	No due date set.
			
				Dependencies
				
				
		
	
	
	No dependencies set.
			Reference: sheetjs/js-crc32#21
			
		
	
		Loading…
	
		Reference in New Issue
	
	Block a user
	
	No description provided.
		
		Delete Branch "%!s()"
	 
	Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
So, I know that this problem has already been raised more than once.
Let's give an example, take a png file in which the checksum is calculated using exactly the same algorithm. There is data:
In the file, this data has the following CRC:
FC18EDA3The same algorithm returns a negative value:
-3E7125DIf you look at other implementations of this algorithm, you will notice that they all return an unsigned value. Even in Python, this problem has already been fixed:
For example, unsigned numbers are used here so that the algorithm works correctly:
http://stigge.org/martin/pub/SAR-PR-2006-05.pdf
Maybe it's worth using
>>> 0to work correctly?Or make a separate function for unsigned calculation🤔
First, the README states that the result is a signed 32 bit integer.
The main reason for sticking with signed 32-bit integers is performance.
V8 (Node/Chrome JS engine) can optimize for "Smi" (small integers). On a 64-bit platform, Smi is signed 32-bit integer. V8 deoptimizes when vacillating between 32-bit signed ints and integers not in the range -231 .. 231 - 1 . This was discussed a bit in https://github.com/SheetJS/js-crc32/issues/4#issuecomment-170050796
The
>>> 0part is easy enough not to merit a separate function.It probably makes sense to add a note in the README about how to coerce to 32-bit signed integer (
x | 0) and how to coerce to 32-bit unsigned integer (x >>> 0), and we'd accept a PR for that.GitHub markdown unfortunately lacks admonitions, but the sentence "The return value is a signed 32-bit integer!" has been bolded and https://github.com/SheetJS/js-crc32#signed-integers includes some conversion notes. Please let us know how we can further improve the docs!