Is there a way to read binary data in JavaScript?

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

i.e.

var binObj = new BinaryObject('101010100101011'); 

Something like that. Any help would be great.

+46
javascript binary
Nov 29 '08 at 16:43
source share
11 answers

You can use parseInt:

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

The second argument (base) is the base for input.

+30
Nov 29 '08 at 16:46
source share

Here is this binary ajax library that is explained here as well as another binary parser library that can handle more data types.

You can also look at Google Gears, which is a Blob binary, or take a look at creating a javascript wrapper for Flash that provides a built-in implementation of ByteArray .

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

+23
Nov 22 '09 at 21:15
source share

In all recent browsers you can:

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

And get the string. To get the binary result, you will need to do

 data.charCodeAt(pos) & 0xff; 



In the nightly builds of Firefox and Chrome, you can get the value as ArrayBuffer

 xhr.responseType = "arraybuffer"; 

Result is then available there

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

You can then apply a TypedArray (for example: Int32Array) or a DataView in the buffer to read the result.




To facilitate this process, I made jQuery Patch to support binary type and DataView Wrapper , which uses the latest available browser read function.

+16
Jan 27 '11 at 11:02
source share

JavaScript has very little support for raw binary data. In general, it is best to live in this restriction. However, there is a trick that I am considering trying to complete my project, which involves manipulating huge raster images to perform operations in an OLAP database. This will not work in IE .

The basic idea is this: forcing binary data to PNG for sending in JavaScript. For example, a bitmap can be black and white PNG, and black can be 100% transparent. Then use Canvas operations to process the data bitwise.

HTML5 Canvas contains a type of pixel array that allows you to access bytes in an image. Canvas also supports layout 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, possibly using a graphics processor.

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

+10
Sep 11 '09 at 16:57
source share

It would be the other way around ... pow and squareroot can be calculated by Math-Class ... I don’t know if this is the fastest way, but it is as fast as the Windows Calculator in the “programmer’s 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(ij != 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; } 
+2
Dec 10 2018-10-10
source share

In the near future, you will be able to use ArrayBuffers and the Blobs API File .

+2
Jan 07 '11 at 23:24
source share
+2
May 11 '12 at 9:23 a.m.
source share

As @Zippy pointed out in a comment, later (latest 2016) solutions include:

+2
Nov 19 '16 at 15:26
source share

Javascript does not provide a mechanism for loading an object in any form other than simple lines.

The closest thing you can do is serialize the object into a string, optionally encrypt / compress it, send it to the browser and decrypt / decompress if necessary, check if it works properly, eval () and pray ().

Instead of using eval (which is not entirely safe), you can use your own format (alternatively, xml or json, for which there are many libraries) and analyze it yourself.

As a side note, if you want this to obfuscate after the browser receives usable data (after decryption / decompression), this is too easy to get around.

+1
Nov 29 '08 at 23:46
source share

Percent encoding can bring unescape strings into a direct 1 ↔ 1 representation of any binary blob and also carry over in different browsers;

 unescape("%uFFFF%uFFFF%uFFFF"); 

Most browsers use this method to insert shellcode into HTML pages; it works great for creating arbitrary binary streams.

+1
May 17 '11 at 6:58 a.m.
source share

jBinary "makes it easy to create, load, analyze, modify and save complex binary files and data structures in the browser and Node.js."

I have not used it, but this is what I found, asking the same question asked here ...

+1
Dec 18 '14 at 22:33
source share



All Articles