Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 905
m3div0 Avatar asked Jun 19 '12 14:06

m3div0


People also ask

How will you convert the string into integer value using JavaScript?

How to convert a string to a number in JavaScript using the parseInt() function. Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.

How do you convert a string to a binary number?

To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .

Which function is used to convert a number to an integer JavaScript?

parseInt() The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

What is parseInt in JavaScript?

The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.


2 Answers

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 ); 
like image 134
Sirko Avatar answered Oct 21 '22 00:10

Sirko


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.

like image 30
Mattias Buelens Avatar answered Oct 20 '22 22:10

Mattias Buelens