如何在 JavaScript 中将数字的二进制表示从字符串转换为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11103487/
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
How to convert binary representation of number from string to integer number in JavaScript?
提问by david
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?
我有一个字符串,例如“01001011”,我需要做的是反转它,所以我使用了.split('')
比.reverse()
,现在我需要将数组作为字符串读取并将其转换为整数。是否可以?
Thanks
谢谢
回答by Sirko
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.
如果要将数组转换回字符串使用 join()
( MDN) 并将字符串转换为整数使用parseInt()
( MDN)。后者的第二个参数是一个可选的基数。
JavaScript will try to determine, what radix to use, but to be sure you should always add your radix manually. Citing from MDN:
JavaScript 将尝试确定要使用的基数,但要确保您应该始终手动添加基数。引用自 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).
如果 radix 未定义或为 0,则 JavaScript 假定如下:
如果输入字符串以“0x”或“0X”开头,则基数为 16(十六进制)。
如果输入字符串以“0”开头,则基数为八(八进制)。这个特性是非标准的,一些实现故意不支持它(而是使用基数 10)。由于这个原因,在使用 parseInt 时总是指定一个基数。
如果输入字符串以任何其他值开头,则基数为 10(十进制)。
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 );
回答by Mattias Buelens
Just call parseInt
with a different radix, in this case use 2 for binary.
只需parseInt
使用不同的基数调用,在这种情况下使用 2 表示二进制。
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:
parseInt
当您没有明确指定基数时,它会尝试找出基数本身。来自 Mozilla 开发者网络:
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 usingparseInt
.- If the input
string
begins with any other value, the radix is 10 (decimal).
如果 radix 是
undefined
或 0,JavaScript 假设如下:
- 如果输入
string
以“0x”或“0X”开头,则基数为 16(十六进制)。- 如果输入
string
以“0”开头,则基数为八(八进制)。这个特性是非标准的,一些实现故意不支持它(而是使用基数 10)。出于这个原因,在使用时总是指定一个基数parseInt
。- 如果输入
string
以任何其他值开头,则基数为 10(十进制)。
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.
在这种情况下,您必须指定基数,否则它可能会被解释为十进制或八进制数。根据经验,始终指定基数。
回答by Cody
This will take a buffer hex and convert it to a binary str and back to the buffer hex.
这将采用缓冲区十六进制并将其转换为二进制 str 并返回缓冲区十六进制。
NOTE: when I say a buffer hex, I mean a decimal value because when you iterate over a buffer and pull each item in the array, it gives you the decimal value (eg: 210, instead of d2).
注意:当我说缓冲区十六进制时,我的意思是十进制值,因为当您遍历缓冲区并拉出数组中的每个项目时,它会为您提供十进制值(例如:210,而不是 d2)。
var buffer - new Buffer([0, 210, 242]); // Node
var buffer - new Buffer([0, 210, 242]); // Node
// var arrayBuffer = new ArrayBuffer(3); // JavaScript
// var arrayBuffer = new ArrayBuffer(3); // JavaScript
// var uint8 = new Uint8Array(arrayBuffer); // JavaScript/ 16Array, 32Array, etc
// var uint8 = new Uint8Array(arrayBuffer); // JavaScript/ 16Array, 32Array, etc
Need to be acquainted with buffers
需要熟悉缓冲区
You'll iterate over the buffer with a for(){}
and inside you can do something like:
您将使用 a 迭代缓冲区,在for(){}
内部您可以执行以下操作:
(210).toString(2); // '11010010'
(210).toString(2); // '11010010'
(210).toString(16); // 'd2' (untested)
(210).toString(16); // 'd2' (untested)
(210).toString(8); // (Octal-Digits representation)
(210).toString(8); // (Octal-Digits representation)
parseInt((210).toString(2), 2); // 210
parseInt((210).toString(2), 2); // 210
parseInt((210).toString(2), 2).toString(16); // 'd2'
parseInt((210).toString(2), 2).toString(16); // 'd2'
Obviously, instead of using "(210).toString(2)
" IN YOU FOR LOOP, you would use "(buffer[i]).toString(2)
"
显然,不是在(210).toString(2)
IN YOU FOR LOOP中使用“ ” ,而是使用“ (buffer[i]).toString(2)
”
Endian Rep is up to you! :) (array.reverse())
Endian 代表由您决定!:) (array.reverse())
Hope this helps!
希望这可以帮助!
PS. parseInt(('00000' + (210).toString(2).substring(5, 8)), 2); // 2
附注。 parseInt(('00000' + (210).toString(2).substring(5, 8)), 2); // 2
parseInt((210).toString(2).substring(5, 8), 2); // 2
parseInt((210).toString(2).substring(5, 8), 2); // 2