Is there a way to read binary data in JavaScript?

Viewed 79898

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.

11 Answers

You can use parseInt:

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

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

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.

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;
}

Javascript doesn't provide a mechanism to load an object in any form other than simple strings.

Closest you can do is serializing the object to a string, optionally encrypting/compressing it, sending it to the browser, and decrypting/decompressing if necessary, checking for sanity, eval() and pray().

Instead of using eval (which is not quite safe), you can use your own format (alternatively, xml or json for which there are plenty of libs) and parse it yourself.

As a side note, if you want this for obfuscation after the browser gets the usable data (after decrypting/decompressing), it is too easy to circumvent.

Welcome to everyone who found this older post on Google. I figured out a solution that works in Chrome as of 2019, so hopefully this is just some added feature or something most people missed.

You can use a 0b prefix to your number. It won't quite get the binary representation, but you can easily convert it to an integer for storage. For example, you could store the binary number 1010 as such:

var binNum = 0b1010 //Stores as an integer, which would be 10

If you're curious, it also works for hexadecimal with the 0x prefix:

var binNum = 0x1010 //Stores as an integer, which would be 4112
Related