Javascript:一个字节应该是8位 [英] Javascript: A byte is supposed to be 8 bits

查看:151
本文介绍了Javascript:一个字节应该是8位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑: http://www.ascii-code.com/ 我看到了BIN列为二进制但我显然遗漏了一些东西..

http://www.ascii-code.com/ I see the BIN column as binary but I am clearly missing something..

为什么二进制转换对我不起作用?

Why doesn't binary conversion work for me?

小写字母b是字符代码98

Lowercase b is character code 98

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

输出

1100010

当输出长度为8时,输出长度为7

The length of the output is 7 when it should be 8

一个字节是8位!!?

编辑


比特组构成字节当8比特组合在一起时,它被称为字节。字节是计算机用来表示各种字符的字节,例如键盘上显示的字符。

Groups of Bits make up a Byte When 8 bits are grouped together, it is then known as a byte. And bytes are what computers use to represent various characters such as those you see on your keyboard.

引用自: http://wordsmuggler.com/Learn/Binary

我现在真的不明白我应该阅读的内容。如果我在谷歌上看到我总是被告知8但在这里我被告知不同。请解释,因为我不明白我应该理解什么

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. Please explain as I don't understand what I am supposed to be understanding

推荐答案

的原因。 toString(2)不产生数字的8位表示, toString 适用于多于0到255的数字。例如:

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:

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

那么JavaScript用 toString(2)做什么只是简单地给你基数为2的数字,即0,1,10,11,100,101等,就像在基数10中我们写出数字0,1,2,3,4一样,5,...我们并不总是填写我们的数字来制作一定数量的数字。这就是为什么你没有在输出中看到8个二进制数字的原因。

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. That is why you are not seeing 8 binary digits in your output.

现在,你要记住的问题是如何取0范围内的数字。 .255并在JavaScript中将其显示为二进制编码的BYTE?事实证明,需要由程序员完成;它不是JavaScript中的内置操作!在base-2中编写数字,并编写8- bit,是相关的问题,但它们是不同的。

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.

要做你想做的事,你可以写一个函数:

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)
}

以下是它的使用方法:

> 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

这篇关于Javascript:一个字节应该是8位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆