Created
December 18, 2010 19:29
-
-
Save stagas/746783 to your computer and use it in GitHub Desktop.
bit compressor in pure javascript
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
| // bit compressor | |
| // by stagas | |
| // public domain | |
| var bitEncode = function(bits) { | |
| var arr = [] | |
| while (bits.length) { | |
| arr.push(parseInt('1' + bits.substr(0, 52), 2).toString(32)) | |
| bits = bits.substr(52) | |
| } | |
| return arr.join('.') | |
| } | |
| var bitDecode = function(str) { | |
| var sarr = str.split('.') | |
| , arr = [] | |
| for (var i = 0, len=sarr.length; i<len; i++) { | |
| arr.push(parseInt(sarr[i], 32).toString(2).substr(1)) | |
| } | |
| return arr.join('') | |
| } | |
| var bits = | |
| '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| + '110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
| var compressed = bitEncode(bits) | |
| console.log('Output:', compressed) | |
| console.log('Down to', (compressed.length / bits.length * 100).toFixed(2), '% of original size') | |
| console.log('Test:', bits == bitDecode(compressed)) |
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
| Output: 7cl9ainqlfl.55qjl9arcl9.5aavalul5qj.6l5bdil5aav.5anqknael5b.5makl9btanq.6it9qkldmak.6l5flavait9.7ailmpail5f.6lbtabl7ail.6r5aaklulbt.59ektaamr5a.5ainqlfl9ek.7l9arcl9ain.7alul5qjl9a.7dil5aavalu.d9ektaam | |
| Down to 22.99 % of original size | |
| Test: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment