0

This is my problem:

  • there is binary file (containing short int - 16 bit) on the server
  • I'd like to load it and create data for graphs (I use http://dygraphs.com/ - it must be native format array [ [0,1], [1,2] ])

I've tried this code:

window.addEvent(
        "domready",
        function()
        {
            var dataRequest = new Request(
            {
                url: "data.dat",
                onSuccess: function(response)
                {
                    var timestart = new Date();

                    var tempArray = response.match(/.{2}/g);

                    for(var i=0; i<tempArray.length; ++i) {

                        graphDataArray[i] = [i, (tempArray[i].charCodeAt(0)<<8) + tempArray[i].charCodeAt(1) ];                 
                    }

                    new Dygraph(
                        document.getElementById("graphdiv"),
                          graphDataArray,
                          {}
                    );

                    document.getElementById("timemsg").innerHTML = "Generated in " + (new Date() - timestart).toString() + " ms";                       
                }
            }
            ).send();
);

It doesn't work because charCodeAt() has nothing to do with binary data, it interprets ASCII. What should I do?

miyagi
  • 23
  • 1
  • 4
  • [`btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/Window.btoa)? – Teemu Mar 28 '14 at 22:02
  • @Teemu Did you mean **atob()**? It returns: "Uncaught InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded contains characters outside of the Latin1 range." – miyagi Mar 28 '14 at 22:18
  • No, I meant `btoa()`, "BinaryTOAscii"... – Teemu Mar 28 '14 at 22:20
  • @Teemu I need conversion from string to integer. But my string contains strange characters. I get data from file as a string and I'd like to "cast" them to 16 bit integer, is it even possible? – miyagi Mar 28 '14 at 22:36
  • I've seen people do some weird things moving binary around -- pretending it is an image file... and the Canvas API can give you access to the binary below. – Jeremy J Starcher Mar 28 '14 at 22:54
  • @Jeremy J Starcher Jeez. Simple task: load file, interpret it as int16, convert to array and create graph with provided API need to be done using Canvas? – miyagi Mar 28 '14 at 23:06
  • @Teemu You can provide "native" format arrays as input for graphs. Something like that: [ [x1,y1], [x2,y2], /*etc.*/ ]. And all I need is to create such array from raw/binary file that consist of 16bit integers. – miyagi Mar 28 '14 at 23:29
  • @miyagi Where and for which application has this binary data been created? Are you sure it can be interpreted to JS at all? – Teemu Mar 28 '14 at 23:47
  • @Teemu I am creating this data. It's output from accelerometer saved to file in raw, binary format - 16bit integer data. Now I am trying to create graphs available on web. Data is not a problem. I am not familiar with JS and all methods that seems to be legit and obvious for other programming languages (casting, conversion etc.) fails. So I asked you for more specific way. – miyagi Mar 29 '14 at 00:35

1 Answers1

0

I've found solution. Here is how to transmit binary data to browser: solution

Then, I simply iterate trough array and use binary shift operator to create 16 bit int and add it to array formated to fit graph library needs:

for (var i = 0; i < byteArray.byteLength; i+=2) {
  graphDataArray[counter] = [counter, (byteArray[i]<<8) + byteArray[i+1] ];
  counter++;    
}
miyagi
  • 23
  • 1
  • 4