7

On ASCII table according to Windows-1252 I see the BIN column as binary, but I am clearly missing something...

Why doesn't binary conversion work for me?

Lowercase b is character code 98, and

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

outputs

1100010

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

A byte is 8 bits!!?

From What is Binary?:

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.

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 differently. What I am supposed to be understanding?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BENZ.404
  • 401
  • 1
  • 5
  • 20
  • 2
    It's not going to pad to a multiple of 8 digits any more than it would pad the output in base 10 or any other base. It's a base 2 representation of the number, not a bit pattern. (The bit pattern is a 64-bit floating-point representation, anyway.) – user2357112 Jun 21 '14 at 00:55
  • 2
    `.toString` is not going to give you all the zeros. Anyway, in JavaScript, a number is not 8 bits, but [64bits](https://en.wikipedia.org/wiki/Double_precision_floating-point_format#IEEE_754_double-precision_binary_floating-point_format:_binary64). – Derek 朕會功夫 Jun 21 '14 at 00:59
  • The canonical is *[How can I pad a value with leading zeros?](https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros)*. 77 answers. From 2009. – Peter Mortensen Nov 29 '22 at 20:25
  • There is also *[Is there a JavaScript function that can pad a string to get to a determined length?](https://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-leng)* – Peter Mortensen Nov 29 '22 at 21:11

2 Answers2

21

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"

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.

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

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
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • I'm doing this all wrong, I don't want to pad. see the BIN column here http://www.ascii-code.com/ I see binary, what is the difference? – BENZ.404 Jun 21 '14 at 01:00
  • 3
    @BENZ.404 - I think you need a better understanding in binary numbers and ascii before asking more questions. – Derek 朕會功夫 Jun 21 '14 at 01:01
  • > 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. Quoted from: http://www.wordsmuggler.com/Learn/Binary – BENZ.404 Jun 21 '14 at 01:33
  • 1
    JavaScript's `toString(2)` simply gives a string in base-2 and has nothing at all to do with packing numbers into bytes. I think the issue here is that you were expecting `toString(2)` to produce a byte-sized encoding of a number, but it just doesn't do that, nor does it promise to. In fact, JavaScript doesn't even have the notion of byte at all! It just has `Number` which is a 64-bit encoding called IEEE-754. It also sometimes drops into 32-bit integer encodings for some operations. TL;DR don't go looking to JavaScript to make 8-bit patterns of "bytes"! It doesn't. – Ray Toal Jun 21 '14 at 05:17
  • So can someone answer as JavaScript doesn't do that so that I can accept and so that anyone else who thinks it simple will stop before they start to try to do this. – BENZ.404 Jun 21 '14 at 12:51
  • Okay, I've updated the answer to say why it is not done and why it is not built-in to JavaScript. I've also given a relatively simple function which I believe will work for you. – Ray Toal Jun 21 '14 at 21:01
2

Here is another solution:

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

It works in a single line just like this:

while(test.length < 7) test = "0" + test
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    This padding is what the OP wants. If dealing with n < 128. Also see String.fromCharCode.apply(null, some uint8Array); (within limits). – mckenzm Nov 06 '21 at 19:13