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.

7
  • take a look at parseInt() in javascript - w3schools.com/jsref/jsref_parseint.asp Commented Jan 15, 2018 at 15:37
  • @RishikeshDhokare can I just apply that straight to the buffer?? Commented Jan 15, 2018 at 15:39
  • 1
    You cant access a buffer directly. You need to use a "view" on it, usually a TypedArray Commented Jan 15, 2018 at 15:39
  • 1
    As nodejs' Buffer already is a Uint8Array, you can work with it like a normal array to get the integers Commented Jan 15, 2018 at 15:44
  • @JonasW. okay so I can just iterate through it and parse the ints from it into a new array? Commented Jan 15, 2018 at 15:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Int32Array doesn't seem to be working for me, do you know how I can find what type they are? Thanks
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.
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?
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.
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
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.