Created
December 18, 2010 20:08
-
-
Save stagas/746835 to your computer and use it in GitHub Desktop.
another bit compressor in pure javascript
This file contains 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
// another bit compressor | |
// by stagas | |
// public domain | |
var bitEncode = function(bits) { | |
var arr = [] | |
while (bits.length) { | |
arr.push(String.fromCharCode(parseInt('1' + bits.substr(0, 15), 2))) | |
bits = bits.substr(15) | |
} | |
return arr.join('') | |
} | |
var bitDecode = function(str) { | |
var sarr = str.split('') | |
, arr = [] | |
for (var i = 0, len=sarr.length, s; i<len; i++) { | |
s = sarr[i].charCodeAt(0).toString(2) | |
s = s.substr(s.indexOf(1) + 1) | |
arr.push(s) | |
} | |
return arr.join('') | |
} | |
var bits = | |
'110110010101010010101010010101111101010101011111010101001011101010011101010100101010110' | |
+ '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 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: ꪕ磌퓪쪶풯핟꩝Ꝕ햶ꪕꕽ틪몥궲풪ꯪퟕ靓픪ꕒ▒뺩몝ꪕ磌퓪쪶풯핟꩝Ꝕ햶ꪕꕽ틪몥궲풪ꯪퟕ靓픪ꕒ▒뺩몝ꪕ磌퓪ᥖ | |
Down to 6.69 % of original size | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment