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
The byte() function in p5. js is used to convert the given string of number, number value or boolean into its byte representation. This byte number can only be whole number in between -128 to 127.
The largest number you can represent with 8 bits is 11111111, or 255 in decimal notation. Since 00000000 is the smallest, you can represent 256 things with a byte.
A byte is 8 bits because that's the definition of a byte. An ASCII character is stored in a byte because trying to use just 7 bits instead of 8 means you cannot address one character directly and would have to pack and unpack bit strings any time you wanted to manipulate text - inefficient, and RAM is cheap.
In most computer systems, a byte is a unit of data that is eight binary digits long. A byte is the unit most computers use to represent a character such as a letter, number or typographic symbol. Each byte can hold a string of bits that need to be used in a larger unit for application purposes.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With