Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created September 7, 2011 14:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jed/1200689 to your computer and use it in GitHub Desktop.
Save jed/1200689 to your computer and use it in GitHub Desktop.
fletcher checksum
function(a,b,c,d,e){for(b=c=d=0;e=a.charCodeAt(d++);c=(c+b)%255)b=(b+e)%255;return c<<8|b}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "fletcher checksum",
"description": "a javascript port of wikipedia's example C implementation of the fletcher checksum",
"keywords": [
"fletcher",
"checksum",
"hash"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>51599</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var fletcher = function(a,b,c,d,e){for(b=c=d=0;e=a.charCodeAt(d++);c=(c+b)%255)b=(b+e)%255;return c<<8|b}
document.getElementById( "ret" ).innerHTML = fletcher("140byt.es is totally rad!")
</script>
@p01
Copy link

p01 commented Sep 7, 2011

return b|c<<8 is 1 byte shorter ;)

@jed
Copy link
Author

jed commented Sep 7, 2011

good catch! tho i don't think i need to reverse the order, do i?

@monolithed
Copy link

fletcher('foo2e2e2') // 166 it's ok ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment