View Single Post
Old 10-13-2012, 03:46 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Is there any point in noting that JavaScript will do this for you?
Code:
<script type="text/javascript">
var binary = "110010111011000011101";
var number =  parseInt( binary, 2 );
alert( number );
</script>
The second argument to parseInt -- the 2 -- tells JS to convert the string in radix=2. That is, in binary.

And of course you can go back the other way:
Code:
var number = 7181711;
var binary = number.toString(2);
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote