Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - opposite of toString()?

When used with numbers, the toString() method converts a number into a string representation using the base numbering system specified by the optional radix.

For instance the number 8 and toString(2) would return "1000".

Is there a method to achieve the opposite? I.e., convert the string "1000" back into the number 8?

like image 866
posfan12 Avatar asked Jan 08 '11 04:01

posfan12


3 Answers

parseInt() takes a radix as its second argument, so you can do parseInt("1000", 2) to get what you want.

like image 142
Paige Ruten Avatar answered Oct 25 '22 16:10

Paige Ruten


You can use parseInt using a raidx 2. i.e

parseInt("1000", 2) will return 8
like image 28
Chandu Avatar answered Oct 25 '22 17:10

Chandu


Check the parseInt() Javascript function: http://www.w3schools.com/jsref/jsref_parseInt.asp

like image 39
turtlepick Avatar answered Oct 25 '22 17:10

turtlepick