JavaScript的:一个字节假设是8位 [英] Javascript: A byte is suppose to be 8 bits

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

问题描述

编辑:的http://www.ascii-$c$c.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表示字符code 98

lowercase b is character code 98

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

输出

1100010

输出的长度是7时,它应为8

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

一个字节为8位!?

修改

比特组组成一个字节当8位组合在一起,那么它被称为一个字节。和字节是计算机用什么来重新present各种字符,如那些你在键盘上看到的。

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 suppose 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 suppose to be understanding

推荐答案

之所以的ToString(2)不产生8位再presentation一些是的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中的二进制恩codeD BYTE?事实证明,需要做的事情由程序员;!它不是在JavaScript中内置的操作在基-2-写入的数,和写入的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.

要你愿意,你可以写一个函数是什么:

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天全站免登陆