Is there a way to read binary data in JavaScript?

I would like to inject binary data into an object in JavaScript. Is there a way to do this?

i.e.

var binObj = new BinaryObject('101010100101011');

Something to that effect. Any help would be great.


Solution 1:

You can use parseInt:

var bin = parseInt('10101010', 2);

The second argument (the radix) is the base of the input.

Solution 2:

There's this binary ajax library that is explained here and there's also another binary parser library that can handle more data types.

You could also look at Google Gears which has a binary Blob object or take a look at making a javascript wrapper for Flash which provides a native ByteArray implementation.

Or... you can sit and wait and hope that all these things become standard :)

Solution 3:

On all recent browsers you can do:

xhr.overrideMimeType('text/plain; charset=x-user-defined');

And retrieve a string. To get the binary result you will have to do

data.charCodeAt(pos) & 0xff;

On the nightly builds of Firefox and Chrome you can retrieve the value as an ArrayBuffer

xhr.responseType = "arraybuffer";

The result is then accessible there

xhr.mozResponseArrayBuffer // Firefox
xhr.response // Chrome

Then you can apply a TypedArray (eg: Int32Array) or a DataView on the buffer to read the result.


In order to make this process easier, I made a jQuery Patch to support the binary type and a DataView Wrapper that uses the latest available reading feature of the browser.

Solution 4:

JavaScript has very little support for raw binary data. In general, it's best to live within this restriction. However, there's a trick I'm considering trying for a project of mine that involves manipulating huge bitmaps to do set operations in an OLAP database. This won't work in IE.

The basic idea is this: coerce the binary data into a PNG to send it to JavaScript, For example, a bitmap might be a black and white PNG, with black being 100% transparent. Then use Canvas operations to do bitwise data manipulation.

The HTML5 Canvas includes a pixel array type, which allows access to bytes in an image. Canvas also supports compositing operations, such as XOR. Lighten and darken should be able to do AND and OR. These operations are likely to be well optimized in any browser that supports them-- probably using the GPU.

If anyone tries this, please let me know how well it works.

Solution 5:

That would be the other way around... pow and squareroot might be calculated by the Math-Class... I don't know if it is the fastest way, but it's as fast as the Windows Calculator in the "Programmer View".

AlertFormatedBin();
function AlertFormatedBin()
{
    var vals = decToBinArr(31,8);
    var i;

    var s = "";
    var mod = vals.length % 4;
    for(i= 0; i <mod;i++)
    {
        s+=vals[i];
    }
    if(i>0)
        s+=" ";
    var j = i;
    for(i;i<vals.length;i++)
    {
        s+=vals[i];
        if(i-j != 0 && (i+1-j)%4 == 0)
        {
            s+=" ";
        }
    }
    alert(s);
}

function decToBinArr(dec, minSize)
{
    var mod = dec%2;
    var r = new Array();
    if(dec > 1)
    {
        dec-=mod;
        var bd = squareRootRoundedDown(dec);
        if(minSize && minSize-1 > bd)
            bd = minSize-1;
        else
            var i;
            for(i = bd; i>0;i--)
            {
                var nxt = pow(2,i);
                if(dec >= nxt)
                {
                    r[i] = 1;
                    dec-=nxt;
                }
                else
                {
                    r[i] = 0;
                }
            }
    }
    r[0]= mod;
    r.reverse();
    return r;
}

function squareRootRoundedDown(dec)
{
    if(dec<2)
        return 0;
    var x = 2;
    var i;
    for(i= 1;true;i++)
    {
        if(x>=dec)
        {
            i = x == dec ? i : i-1;
            break;
        }
        x= x*2;
    }
    return i;
}

function pow(b,exp)
{
    if(exp == 0)
        return 0;
    var i = 1;
    var r= b;
    for(i = 1; i < exp;i++)
        r=r*b;
    return r;
}