Sheldon Pinkman
Sheldon Pinkman

Reputation: 1106

javascript compression algorithm that supports BINARY data?

I'm looking for a lossless compression algorithm (like LZW or Huffman or anything) in javascript, that accepts and returns raw, binary data.

With 'binary data' I mean a sequence of bytes, which may come in any of the following forms:

Now obviously there are TONS of javascript implementations available everywhere, for a wide range of algorithms. However EVERYTHING I find seems to do crazy stuff like:

Would anyone know of a good javascript compression (+decompression) implementation that suits my binary fetish?

Upvotes: 6

Views: 4951

Answers (2)

Sheldon Pinkman
Sheldon Pinkman

Reputation: 1106

I think I found what I was looking for after all: this deflate + inflate implementation in javascript seems to work with strings as byte sequences.

Upvotes: 1

Dasarp
Dasarp

Reputation: 194

first of all create a closure for hold the binar or hex or decimal flags

function ASearch() { }
ASearch.Flag = {
    Front_Wheel_Drive: 0xF, Rear_Wheel_Drive: 0xF0, Four_Wheel_Drive: 0xF00,
    Auto: 0xFF, Manual: 0xFF00,
    Gas: 0xF, Diesel: 0xF0, Hybrid: 0xF00, Electrical: 0xF000,
    Two: 1, Three: 2, Four: 4, Five: 8, Six: 16, Eight: 32, Ten: 64, Twelve: 128
};

then set like this

SetFlag = (function (e) {
   e = e.srcElement;
   $("#" + e.parentNode.name).val(e.checked ?
        $("#" + e.parentNode.name).val() | ASearch.Flag[e.id] :
        $("#" + e.parentNode.name).val() ^ ASearch.Flag[e.id]);
});

this is an example for packed data in a 32 bit integer

there are four variable... i've used them for 18 flags.. this is fast and super effective

for example...

int i = 0; //binary = 0000 0000 0000 0000

i = i | 255; //binary = 0000 0000 1111 1111

i = i ^ 255; //binary = 0000 0000 0000 0000

i = i | 0xFF00; //binary = 1111 1111 0000 0000

i = i | 255; //binary = 1111 1111 1111 1111

i = i ^ 0xFF00; //binary = 0000 0000 1111 1111

Upvotes: -3

Related Questions