27

A newbie here! Wondering why the following conversion fails!

var num = prompt("Enter num");
alert(num.toString(2));

If num input is 32. I get 32 as num alert message too.

2
  • 1
    That's what I'd expect to happen. What are you expecting this to do that it doesn't do?
    – greg84
    Commented Apr 21, 2013 at 15:39
  • To convert 32 as a string that displays the binary number!
    – Ken
    Commented Apr 21, 2013 at 15:41

5 Answers 5

43

try

(+num).toString(2)

,

Number(num).toString(2)

or

parseInt(num, 10).toString(2)

Any of those should work better for you.

The issue is that the toString method of javascript Number objects overrides the toString method of Object objects to accept an optional radix as an argument to provide the functionality you are looking for. The String object does not override Object's toString method, so any arguments passed in are ignored.

For more detailed information about these objects, see the docs at Mozilla:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String#Methods

or W3 schools:

http://www.w3schools.com/jsref/jsref_tostring_number.asp http://www.w3schools.com/jsref/jsref_obj_string.asp

9
  • So this does the same right? It converts the string 32 into an integer and then do the binary conversion..
    – Ken
    Commented Apr 21, 2013 at 15:48
  • why not just decnum.toString(2) without all the foreplay
    – Shai UI
    Commented Jul 3, 2014 at 14:44
  • @foreyez because prompt returns a String, and String::toString doesn't take a radix
    – Andbdrew
    Commented Jul 3, 2014 at 17:25
  • I mean "var decnum=123; alert(decnum.toString(2));"
    – Shai UI
    Commented Jul 3, 2014 at 18:47
  • @foreyez sure, that'll work. The question is about a string (that just happens to called num) though.
    – Andbdrew
    Commented Jul 3, 2014 at 20:58
20

With this function you can specify length of the output.

For example decbin(7,4) produces 0111.

function decbin(dec,length){
  var out = "";
  while(length--)
    out += (dec >> length ) & 1;    
  return out;  
}

demo

7

Here is my solution that does not use parseInt, but rather a method that shows the logic behind the conversion of decimal to binary.

This method prints the bits to the array which you may later print out if you wish:

var number = 156;
var converted = [];

while(number>=1) {
    converted.unshift(number%2);
    number = Math.floor(number/2);
}

The converted array will now appear like so:

[1,0,0,1,1,1,0,0]

which of course converts back to 156.

6

/** Convert a decimal number to binary **/

var toBinary = function(decNum){
    return parseInt(decNum,10).toString(2);
}

/** Convert a binary number to decimal **/

var toDecimal = function(binary) {
    return parseInt(binary,2).toString(10);
}

Finally use it

var num= prompt("Enter num"); 
alert(toBinary(num));
5

Cast it to an integer first. At the moment you're converting a string to it's binary representation.

num = +num;
2
  • Convert it to an int. Then Javascript can convert it to a binary string. Commented Apr 21, 2013 at 15:46
  • Okay. please mark either of the answers as correct. Glad we could help. Commented Apr 21, 2013 at 15:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.