Byte Array In Javascript

somebody asked me today why the length

of a hex string is always double the

amount of bytes that it's encoded so I

thought I would make a quick video

explaining what a bit a byte and a hex

string actually are start out by looking

at base 2 which is also called binary

and let's look at a 1 bit number so you

can think of a bit a little bit like an

empty slot and in that slot can either

be a 0 or it can be a 1 and there if you

think about that there can only be two

different things that can go in that

slot a 0 or 1 so there's only 2

different total combinations that one

bit can possibly encode or another way

to say 2 is 2 to the first power so if

we look at a 2 bit number so we have 1 2

empty slots

well now this can either have 0 0 it

could either have 0 1 1 0 or 1 1 so

there's four different or two to the two

different numbers that a two bit number

can encode and then you know this keeps

going on and on and on so if we look at

a 3 bit number 1 2 3 slots and this can

go all the way up to 1 1 1 or 2 to the

third power or eat different numbers

that a 3 bit number could encode ok so

let's look at an 8 bit number so we're

looking at an eight bit number in binary

still and there will be 1 2 3 4 5 6 7 8

different slots that an 8-bit number can

have and if you think about the range of

numbers that an 8-bit number can encode

it can go from 0 0 0 0 0 0 0 0 all the

way up to 1 1 1 1 1 1 1 1 and this is

the range of numbers that an 8-bit

binary number can encode and this would

be 2 to the 8th or 256 different

combinations could live in this range

and we call this an 8-bit binary number

a byte

and one byte can encode 256 different

combinations of numbers so now let's

look at base 10 which is something that

everyone should be familiar with because

this is what normal numbers are and we

could say that the range of numbers that

one byte can encode in base 10 is zero

up to 255 and because this is 256

different combinations with the zero and

this only takes three digits to

represent whereas in binary this would

take eight digits to represent so it's a

lot more efficient to encode and talk

about these numbers in base 10 than

having to write out the binary each time

having these really big numbers okay so

now let's look at an interesting

property if we take the square root of

256 we get 16 and let me erase this over

here and instead of base 2 or base 10

let's look at the square root of 256

which would be beef 16 and this is also

known as hex so in base 16 you not only

have 0 1 2 3 4 5 6 7 8 9 we also have

ABCDEF so there's 16 different digits

that you can use and if you look at a

number like a 7 for example so if you

have the hex number a 7 this is saying

okay this is the 16 slots and then this

is the 1 slot so a is 10 so that means

you have 10 16 and then 7 ones so the

equivalent in base 10 would be the

number 167 for a 7 and likewise the top

end of this range for base 10 255 so 255

we can say that how many 16 slots is

this well 15 times 16 is 240 and then 15

ones on top of that would be 255 total

so you could say there's 15 16s and 15

one in order to get the number 255 so

the top end of this range

for one byte in hexadecimal would be FF

and what's interesting here is that this

is only two characters versus three

characters in base pack so you could

effectively say that any bite can be

represented in two hexadecimal

characters any one bite can be

represented in two hexadecimal

characters and you see this you probably

encountered this in your day-to-day life

as a web developer for example if you've

ever used CSS you know that colors right

colors are represented as three bytes

because you need the byte value for read

use the right value for green and the

red value for blue and this can

represent most colors that you see on

the web but there's multiple different

ways to represent it there's these

values called like RGB which are base 10

and then these values called hex which

are obviously base 16 so you might see

something like two five five one six

seven one nine as an RGB value and this

maps to its equivalent hex value to five

five we said with FF 167 we know is a

seven and then 19 would be well it's 116

plus three ones so that would be one

three and these are equivalent these are

just different ways to represent the

value of bytes in hex versus base 10

it's just shorter in hex because you can

encode any bite into two hex characters

so for example in a cerium now where you

might encounter hex values let's look at

an example of something like an

aetherium address and an aetherium

address is exactly 20 bytes long so 20

bytes of the address can be encoded as a

string of exactly 40 characters right

because two hex characters one bites you

might see a 309 C 716 whatever right and

this string will be 40 characters long

but it'll actually be 42 because the 0x

at the beginning this is just an

indicator that the string to follow is

in code

v16 hacks so 0x is just an indicator to

certain programs that the following

encoding is a sixteen and you can also

you'll see this a lot of times broken up

into what are called byte arrays so

you'll just have each individual byte as

its own object so like you'll just have

it represented like a three space oh

nine space C seven space sixteen etc so

you might see it in address represented

like this which is just a series of

bytes encoded in hex and also in

JavaScript with es6 we call these

buffers so you can represent data not

only as a hex encoded string of exactly

a specific amount of characters but in

buffers and byte arrays as well so

anyway that was a quick answer to why

the length of strings is always double

the amount of light in there iam and I

hope that that's helpful