How to convert binary representation of number from string to integer number in JavaScript?

Can anybody give me a little advice please?

I have a string, for example "01001011" and what I need to do is to reverse it, so I used .split('') than .reverse() and now I need to read the array as a string and convert it to integer. Is it possible?

Thanks


Solution 1:

If you want to convert the array back to a string use join() (MDN) and for converting a string to an integer use parseInt() (MDN). The second argument of the later is an optional radix.

JavaScript will try to determine, what radix to use, but to be sure you should always add your radix manually. Citing from MDN:

If radix is undefined or 0, JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).

  • If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.

  • If the input string begins with any other value, the radix is 10 (decimal).

So in your case the following code should work:

var a = '01001011';

var b = parseInt( a.split('').reverse().join(''), 2 );

or just (if you would want to convert the starting string, without the reversal):

var b = parseInt( a, 2 );

Solution 2:

Just call parseInt with a different radix, in this case use 2 for binary.

var a = parseInt("01001011", 2);
// a === 75

parseInt attempts to figure out the radix itself when you don't explicitly specify it. From the Mozilla Developer Network:

If radix is undefined or 0, JavaScript assumes the following:

  • If the input string begins with "0x" or "0X", radix is 16 (hexadecimal).
  • If the input string begins with "0", radix is eight (octal). This feature is non-standard, and some implementations deliberately do not support it (instead using the radix 10). For this reason always specify a radix when using parseInt.
  • If the input string begins with any other value, the radix is 10 (decimal).

In this case, it is crucial that you do specify the radix, as otherwise it may be interpreted as either a decimal or an octal number. As a rule of thumb, always specify the radix.

Solution 3:

This will take a buffer hex and convert it to a binary str and back to the buffer hex.

NOTE: when I say a buffer hex, I mean a decimal value because when you iterate over a buffer and pull each item in the array, it gives you the decimal value (eg: 210, instead of d2).

var buffer - new Buffer([0, 210, 242]); // Node

// var arrayBuffer = new ArrayBuffer(3); // JavaScript

// var uint8 = new Uint8Array(arrayBuffer); // JavaScript/ 16Array, 32Array, etc

Need to be acquainted with buffers

You'll iterate over the buffer with a for(){} and inside you can do something like:

(210).toString(2); // '11010010'

(210).toString(16); // 'd2' (untested)

(210).toString(8); // (Octal-Digits representation)

parseInt((210).toString(2), 2); // 210

parseInt((210).toString(2), 2).toString(16); // 'd2'

Obviously, instead of using "(210).toString(2)" IN YOU FOR LOOP, you would use "(buffer[i]).toString(2)"

Endian Rep is up to you! :) (array.reverse())

Hope this helps!

PS. parseInt(('00000' + (210).toString(2).substring(5, 8)), 2); // 2

parseInt((210).toString(2).substring(5, 8), 2); // 2