0

I have a c# application that converts a double array to a byte array of data to a node.js server which is converted to a Buffer (as convention seems to recommend). I want to convert this buffer into an array of the numbers originally stored in the double array, I've had a look at other questions but they either aren't applicable or just don't work ([...buf], Array.prototype.slice.call(buf, 0) etc.).

Essentially I have a var buf which contains the data, I want this to be an array of integers, is there any way I can do this?

Thank you.

Vicci Garner
  • 45
  • 2
  • 7

2 Answers2

1

First, you need to know WHAT numbers are in the array. I'll assume they are 32bit integers. So first, create encapsulating Typed Array around the buffer:

 // @type {ArrayBuffer}
 var myBuffer = // get the bufffer from C#
 // Interprets byte array as 32 bit int array
 var myTypedArray = new Int32Array(myBuffer);
 // And if you really want standard JS array:
 var normalArray = [];
 // Push all numbers from buffer to Array
 normalArray.push.apply(normalArray, myTypedArray);

Note that stuff might get more complicated if the C#'s array is in Big Endian, but I assume it's not. According to this answer, you should be fine.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Int32Array doesn't seem to be working for me, do you know how I can find what type they are? Thanks – Vicci Garner Jan 15 '18 at 16:15
  • Can you look into the C# application source code? Or you can use trial end error - on MDN you can find list of typed array classes, just try every each of them. – Tomáš Zato Jan 15 '18 at 16:41
  • I've tried each and every data type, I am expecting 1, 2, 3, 4, 5 however I get 0,0,0,0,0,0,240,63,0,0,0,0,0,0,0,64,0,0,0,0,0,0,8,64,0,0,0,0,0,0,16,64,0,0,0,0,0,0,20,64 for each datatype (except Int8Array which replaces the 240 with -16) when I print the array out toString, any ideas why? – Vicci Garner Jan 24 '18 at 10:52
  • Can you provide me with the array in it's binary form? Or as a list of bytes? Or maybe with the code of the C# application? I can't help you without knowing the data format. – Tomáš Zato Jan 24 '18 at 14:13
  • C#: double[] post = new double[] {1, 2, 3, 4, 5} is how the array is formed, it's then converted into a byte[] array with byte[] postArray = new byte[post.Length * 8] then Buffer.BlockCopy(post, 0, postArray, 0, postArray.Length), this is then sent to the node.js server with a webclient – Vicci Garner Jan 24 '18 at 14:30
  • inside node.js it's formed into a buffer with req.on('data', function(data){ var buf = new Buffer(0); buf = Buffer.concat([buf, data]); etc.. – Vicci Garner Jan 24 '18 at 14:33
0

I managed to do this with a DataView and used that to iterate over the buffer, something I'd tried before but for some reason didn't work but does now.

Vicci Garner
  • 45
  • 2
  • 7