Skip to content

Instantly share code, notes, and snippets.

@stagas
Created December 18, 2010 20:08
Show Gist options
  • Save stagas/746835 to your computer and use it in GitHub Desktop.
Save stagas/746835 to your computer and use it in GitHub Desktop.
another bit compressor in pure javascript
// 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))
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