简体   繁体   English

Javascript:一个字节应该是 8 位

[英]Javascript: A byte is supposed to be 8 bits

EDIT: http://www.ascii-code.com/ I see the BIN column as binary but I am clearly missing something..编辑: http : //www.ascii-code.com/我将 BIN 列视为二进制,但我显然遗漏了一些东西..

Why doesn't binary conversion work for me?为什么二进制转换对我不起作用?

Lowercase b is character code 98小写 b 是字符代码 98

console.log((98).toString(2));

outputs产出

1100010

The length of the output is 7 when it should be 8输出的长度为 7 时应为 8

A byte is 8 bits!!?一个字节是 8 位!!?

EDIT编辑

Groups of Bits make up a Byte When 8 bits are grouped together, it is then known as a byte.位组构成一个字节 当 8 个位组合在一起时,它被称为一个字节。 And bytes are what computers use to represent various characters such as those you see on your keyboard.字节是计算机用来表示各种字符的,例如您在键盘上看到的字符。

Quoted from: http://wordsmuggler.com/Learn/Binary引自: http : //wordsmuggler.com/Learn/Binary

I really don't understand now what I am supposed to read.我现在真的不明白我应该读什么。 If I look on Google I always am told 8 but here I am told different.如果我在谷歌上查看,我总是被告知 8,但在这里我被告知不同。 Please explain as I don't understand what I am supposed to be understanding请解释,因为我不明白我应该理解的内容

The reason why .toString(2) does not produce an 8-bit representation of a number is that toString works for more numbers than just 0 through 255. For example: .toString(2)不产生数字的 8 位表示的原因是toString适用于更多的数字,而不仅仅是 0 到 255。例如:

(1).toString(2) ==> "1"
(2).toString(2) ==> "10"
(3).toString(2) ==> "11"
(4).toString(2) ==> "100"
(25).toString(2) ==> "11001"
(934534534).toString(2) => "110111101100111101110110000110"

So what JavaScript is doing with toString(2) is simply giving you numbers in base 2, namely 0, 1, 10, 11, 100, 101, etc., the same way that in base 10 we write our numbers 0, 1, 2, 3, 4, 5, ... and we don't always pad out our numbers to make a certain number of digits.所以 JavaScript 对toString(2)所做的只是简单地给你以 2 为基数的数字,即 0、1、10、11、100、101 等,就像在以 10 为基数的我们写数字 0、1、 2, 3, 4, 5, ... 我们并不总是把我们的数字填充成一定数量的数字。 That is why you are not seeing 8 binary digits in your output.这就是为什么您在输出中看不到 8 个二进制数字的原因。

Now, the problem you have in mind is "how do I take a number in the range 0..255 and show it as a binary-encoded BYTE in JavaScript? It turns out that needs to be done by the programmer; it is not a built-in operation in JavaScript! Writing a number in base-2, and writing an 8-bit, are related problems, but they are different.现在,您想到的问题是“如何在 0..255 范围内取一个数字并在 JavaScript 中将其显示为二进制编码的 BYTE?事实证明这需要由程序员完成;它不是JavaScript 中的内置操作!以 2 为基数编写数字和编写 8 位数字是相关的问题,但它们是不同的。

To do what you would like to, you can write a function:为了做你想做的事,你可以写一个函数:

function byteString(n) {
  if (n < 0 || n > 255 || n % 1 !== 0) {
      throw new Error(n + " does not fit in a byte");
  }
  return ("000000000" + n.toString(2)).substr(-8)
}

Here is how it can be used:以下是它的使用方法:

> byteString(-4)
Error: -4 does not fit in a byte
> byteString(0)
'00000000'
> byteString(7)
'00000111'
> byteString(255)
'11111111'
> byteString(256)
Error: 256 does not fit in a byte

Here is another solution :这是另一个解决方案:

test = "11001";
while(test.length<7){
  test="0"+test;
}

It works in a single line just like that :它在一行中工作,就像这样:

while(test.length<7)test="0"+test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM