Javascript: A byte is supposed to be 8 bits
Solution 1
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
Solution 2
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
BENZ.404
Dogecoin mining http://doge.smartcoinpools.com Vertcoin mining http://vtc.smartcoinpools.com MPOS Pools include a unique 'while you mine' experience: Help Desk Ticketing Support 24/7 Coin specific Twitter news feeds Live Coin/BTC Price charts
Updated on June 09, 2022Comments
-
BENZ.404 almost 2 years
EDIT: 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?
Lowercase b is character code 98
console.log((98).toString(2));
outputs
1100010
The length of the output is 7 when it should be 8
A byte is 8 bits!!?
EDIT
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://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. Please explain as I don't understand what I am supposed to be understanding
-
BENZ.404 almost 10 yearsI'm doing this all wrong, I don't want to pad. see the BIN column here ascii-code.com I see binary, what is the difference?
-
Derek 朕會功夫 almost 10 years@BENZ.404 - I think you need a better understanding in binary numbers and ascii before asking more questions.
-
BENZ.404 almost 10 years> 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: wordsmuggler.com/Learn/Binary
-
Ray Toal almost 10 yearsJavaScript'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 expectingtoString(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 hasNumber
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. -
BENZ.404 almost 10 yearsSo 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.
-
Ray Toal almost 10 yearsOkay, 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.
-
mckenzm over 2 yearsThis padding is what the OP wants. If dealing with n < 128. Also see String.fromCharCode.apply(null, some uint8Array); (within limits).