0

I want to send the data captured from my microphone (converted to unsigned 16bit integers) in the browser to my server as a binary string, but I'm having a hard time doing that.

I tried using String.fromCodePoint on my array, but it seems that the resulting String is not a valid one. I also tried using DataView, but not sure how to get a binary String out of that either.

Does anyone know how that can be achieved?

EDIT: I am referring to binary data, as is "binary file", and not to "binary representation of an integer".

mhaligowski
  • 2,182
  • 20
  • 26

1 Answers1

0

This did not answer the question. If only in the browser you can use the btoa function to encode to a base64 string.

Edit: I am assuming you have an array of integers.

Final Edit: Maybe try something like:

arr.map(function(item) {    
    var value = item.toString(2);    
    var padLength = 16 - value.length;    
    var binaryVal = "0".repeat(padLength) + value;

    return binaryVal;
}).join("");
Nathan Montez
  • 461
  • 3
  • 8
  • I have an array of unsigned 16bit integers (0 to 65535), `btoa` creates base64 encoded string from other string. Also, is it necessary to do base64? I'm handling raw binary in the server side anyway? – mhaligowski Jul 11 '17 at 18:05
  • You can pass an array (or at least I was able to in my testing). It isn't difficult to decode a base64 string on the server, but the decoded string would look something like `"1,2,3,4,5"` so it would also require some string splitting which might not work well for your situation. Especially if the array size is very large. – Nathan Montez Jul 11 '17 at 18:09
  • Maybe look at this [answer](https://stackoverflow.com/questions/9954757/javascript-convert-integer-to-array-of-bits) and see if it works for you? – Nathan Montez Jul 11 '17 at 18:11
  • having array printed to string is not what I want. the answer you're referring to is about converting an integer to it's base 2 representation, so it's something else. thanks for your effort, though! – mhaligowski Jul 11 '17 at 18:22
  • I updated my answer as I now understand your question – Nathan Montez Jul 11 '17 at 18:24