Javascript 中的字符串转换(十进制到二进制)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16132905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:22:45  来源:igfitidea点击:

String Conversion in Javascript (Decimal to Binary)

javascript

提问by Ken

A newbie here! Wondering why the following conversion fails!

这里是新手!想知道为什么以下转换失败!

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

If d input is 32.. I get 32 as d alert message too :/

如果 d 输入是 32 .. 我也收到 32 作为 d 警报消息:/

回答by Andbdrew

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 toStringmethod of javascript Numberobjects overrides the toStringmethod of Objectobjects to accept an optional radix as an argument to provide the functionality you are looking for. The Stringobject does not override Object's toStringmethod, so any arguments passed in are ignored.

问题是toStringjavascriptNumber对象的toString方法覆盖了对象的方法,Object以接受一个可选的基数作为参数来提供您正在寻找的功能。该String对象不会覆盖ObjecttoString方法,因此任何传入的参数都将被忽略。

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

有关这些对象的更多详细信息,请参阅 Mozilla 上的文档:

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

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:

或 W3 学校:

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

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

回答by Ken

With this function you can specify length of the output.

使用此功能,您可以指定输出的长度。

For example decbin(7,4)produces 0111.

例如decbin(7,4)产生0111.

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

demo

演示

回答by Max

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

这是我的解决方案,它不使用 parseInt,而是一种显示十进制到二进制转换背后逻辑的方法。

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]

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

which of course converts back to 156.

这当然会转换回 156。

回答by Dipesh KC

/** 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));

回答by christopher

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

首先将其转换为整数。目前,您正在将字符串转换为二进制表示。

num = +num;