binary - Javascript: A byte is suppose to be 8 bits -
edit: http://www.ascii-code.com/ see bin column binary missing something..
why doesn't binary conversion work me?
lowercase b character code 98
console.log((98).tostring(2)); outputs
1100010 the length of output 7 when should 8
a byte 8 bits!!?
edit
groups of bits create byte when 8 bits grouped together, known byte. , bytes computers utilize represent various characters such see on keyboard.
quoted from: http://wordsmuggler.com/learn/binary
i don't understand suppose read. if on google told 8 here told different. please explain don't understand suppose understanding
the reason why .tostring(2) not produce 8-bit representation of number tostring works more numbers 0 through 255. 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 javascript doing tostring(2) giving numbers in base of operations 2, namely 0, 1, 10, 11, 100, 101, etc., same way in base of operations 10 write our numbers 0, 1, 2, 3, 4, 5, ... , don't pad out our numbers create number of digits. why not seeing 8 binary digits in output.
now, problem have in mind "how take number in range 0..255 , show binary-encoded byte in javascript? turns out needs done programmer; not built-in operation in javascript! writing number in base-2, , writing 8-bit, related problems, different.
to to, can write function:
function bytestring(n) { if (n < 0 || n > 255 || n % 1 !== 0) { throw new error(n + " not fit in byte"); } homecoming ("000000000" + n.tostring(2)).substr(-8) } here how can used:
> bytestring(-4) error: -4 not fit in byte > bytestring(0) '00000000' > bytestring(7) '00000111' > bytestring(255) '11111111' > bytestring(256) error: 256 not fit in byte javascript binary
No comments:
Post a Comment