Hey everybody what's going on this is Caleb with slopes dotcom and this is part two of our binary conversion
app.
In this video we're going to create a class called binary decimal and it's basically going to allow
us to just create a custom type that can contain the bytes that we need to make one bit of binary data
and it can also contain an integer value a decimal.
We're going to use one class to contain both.
Really we should be using you know one class for a binary digit and one class for a decimal digit.
However we're going to be using one class for both.
So go ahead and let's open up the side here.
Right click on binary and then click new file.
Ok this is just going to be a swift file and I'm going to call it a binary decimal.
OK.
And you don't want let's say let's organize our code a little bit.
Let's create a new group and let's create one for controller related files and then we can create one
for Model layer files like our binary decimal.
That's a model file.
So open it up.
And what we're going to do is we're going to create a class called binary decimal.
Like I said put some curly brackets.
And now we need to basically create some properties.
So one byte of binary data has 8 bits.
Right.
So we're going to create a property called bits.
So type var bits.
All righty.
And that is going to be an array of type int and we'll explain why in a little bit.
But it's going to be an array of type int and whip's an array of type and it's going to be optional.
OK because we are going to instantiate it when we create it.
So it's going to be optional from the beginning.
Until we give it a value.
OK.
So then we're going to go ahead and create a property called integer var integer of type int and it's
also going to be optional.
OK.
Now we have the integer is going to store the decimal value for this particular number and the bits
property is going to store an array of all of the zeros and ones for the binary data.
I found that's the easiest way to do it for now.
There may be a more efficient way and you could probably discover that if you dove into it.
So next we need to create some initializers and there are two ways that we're going to create one of
these digits.
OK.
One is either by passing in an array of bits and another one is by passing a decimal value.
So let's go ahead and create two initializers let's do innit.
And we're going to initialize it with bits k.
And of course like before that's an array of type int.
All righty.
And if we pass in an array of bits we're going to set self up bits to be equal to bits.
Now we can also initialize one of these by passing in a decimal.
OK so we can say init decimal.
And that's going to be a straight up integer.
So type int.
And inside of this initializer we're going to set self integer to be equal to the decimal value that
we pass in k.
Easy as that.
Now we have two ways to initialize our class.
Pretty cool.
So beneath that what we're going to do is we are now going to write two functions one that's going to
calculate the integer value for a particular array of bits.
And the other function is going to calculate the binary value for a particular integer.
So it's going to do the opposite.
We're going to call those interchangeably when we tap on the button.
Pretty cool stuff.
So let's begin by actually calculating the binary value for a particular integer and then we're going
to dive into calculating the integer value for a particular bit.
OK so here we go let's write our function here func calculate binary value for int k and this is going
to return a string and you might be wondering well why a string.
Well we have to display it in a text field so let's go ahead and return it in the the type that we need.
So we're going to go ahead and first create all of the rows we need for binary and if you think about
what we learned about binary There are 8 bits in one byte of data.
So we're going to create our eight slots and if you remember going from the right side all the way over
to the left it starts at one then two then four and it doubles all the way up to 128 for one byte.
So let's go ahead and create an array called rows so type let rows and that's going to be an array of
all these values 128 K we're going to start there 64 32 16 8 4 2 and 1.
So those are all of our values that we're going to use as placeholders for wherever our binary values
are going to go.
And then what we're going to do is we're going to go ahead and create another array called binary rows
and then basically we're going to do some checking to see if the decimal number because remember we're
calculating the binary value for an integer.
If that integer value fits into one of these rows then we're going to basically need to return a one
to that row right.
So to do that we're going to go ahead and cycle through all the rows we're going to basically say hey
are integer we're going to compare it to the row.
If our value like let's say that we were doing 1 45.
Right.
So that's bigger than all of the channel so you know 128 for sure will be filled if 128 or sorry if
145 minus 128 is greater than or equal to zero.
We can return a 1 because we know that channel will be filled otherwise if it goes below zero.
We know that that channel is too big or it doesn't fit in that channel so we return a zero.
Pretty cool stuff.
So we have our binary rows here now let's create a variable called binary rows and we're basically going
to append the numbers to that.
So go ahead and type var binary rows and that's going to be an array of type int and we need to explicitly
declare it here because we're returning integers to it.
Swift will yell at us if we don't.
So just go ahead and create an empty array of type int and then what we're going to do is we're going
to cycle through all the rows with a for loop check them to see whether they should be a 1 or 0.
We'll get into that in a second.
And then we're basically going to go ahead and append that particular digit 0 or 1 to to our array of
binary rows right here.
So go ahead and type four row in rows and then for each row.
What we're going to do is we're basically going to see hey should this be a 1 or 0.
And we can easily do that with a quick little function Let's actually write it below here funk 1 or
0.
And we're going to go ahead and check for a value and we'll just call it value of type int and then
we're going to say with bit value.
And then get value there for the internal parameter of type int and we're going to return an int and
at the end of course 0 or 1.
So we're passing in a value and we're passing in a bit value like 128 64 32.
And what we're going to do is we're going to check hey if our value if like one hundred forty five minus
the bit value 128 is greater than or equal to zero.
OK we know that we should be returning a one because it fits in that channel.
It's bigger than the channel that we're comparing it against.
Otherwise OK we're going to return zero.
So think about this 145.
If we do 145 minus 128 we get 17.
Then if we were to check again case 17 is 17 minus next channel is 64 17 minus 64 is that greater than
or equal to zero.
No.
So we're going to return to zero next channel 32 17 minus 32 still zero.
OK so we return 0 are still less than zero.
Then we get to 16 17 minus 16 is greater than or equal to zero.
Yes so we return a 1.
Then we go all the way down and we end up with a 1 in the 1 channel 1 and 16 and 1 128 to make 145.
Pretty cool stuff.
So this little function can determine whether or not it should be a 1 or 0.
So for Roe and Rose we're going to create a binary digit by typing let binary digit and that's going
to be equal to the result of 1 or 0 for value.
And what's our value.
Our value is int member because when we create this we pass in an integer value.
So we're going to use our integer from here.
We're going to use the bit value k of the row because remember we're cycling through every one of these
rows and we're comparing them then whatever we get back.
We're going to append two binary rows.
So go ahead and type binary rows pens and then type binary digit because that's what we just created.
Now let's just build this and make sure that we're good we're getting an error here.
Let's make sure we're paying attention to this along the way and it's saying something is not unwrapped.
Although I'm not sure why.
Oh.
Our integer value is not unwrapped.
And we should make sure that it is.
Let's see is there a better way to do this.
Let's see.
Well we can't create an instance of this without it being instantiated.
Oh I guess we can.
So you know what let's go ahead and let's just use guard let here and call it int equals integer else
we'll return.
This is just a safe way to make sure that we have a value and we'll pass an int instead.
And if we build this oh it's going to return.
Let's return maybe error just for now it's not a great return it's not good air handling but it's better
than what we had.
So we now have determined whether all the values are ones or zeros or not and that's cool.
So now what we need to do is we need to actually print out what we have.
And this is just the way we can check to see what's going on.
So we can just print maybe binary rows and that will show us whether or not what we have is good.
So I want to actually check it out.
So let's go into our view controller here.
Let's set it up here with an instance let's just say let Bin digit equals binary digit or binary decimal
I suppose.
And let's pass in a decimal value of 145 K and then we're going to go ahead and print bin digit dot
calculate binary value for int and let's see what we get.
Build and run.
Oh sorry we still need to return a string here.
Let's return by Mary Rose.
And you know what we can actually cast this as a string whoopsies.
There we go.
This is just temporary for now.
Sorry and swift for you need to say string describing.
And now we can print out that array.
So let's go see what we did.
Let's see how it worked for us and let's check it out.
So yeah look at that.
We typed in 145 and we're getting 1 1 1 1 1 1.
All these ones which actually equals 255 in binary.
Like I said it should be 128 16 and 1 but that's not working.
So let's think about what is happening here.
We are passing in our integer value of 145 and we're basically comparing it and we're appending all
of the values to what we're doing let's think oh you know what I just realized what the problem is we're
comparing 145 to every one of these channels.
We're actually we're going to need variables that we can subtract the values as we go through the for
loop so get rid of this guard let And let's just create a variable called new int and that's going to
be equal to integer.
OK.
And you know what we can actually force and wrap it here we know we're going to have a value where we're
going to pass in new int that shouldn't give us a problem.
It's just telling us it was never mutated.
So here's what we need to do after we check to see if the first value 128 should be a 1 or 0.
What we need to do is we actually need to subtract the value of the row.
Right because if 128 is a yes it should be on.
We should subtract that so then we can compare what's left over to the next place.
So go ahead and basically say if the binary digit return is a 1.
OK we're gonna go ahead and say new int equals new int minus Rho.
And that way new it gets smaller every single time.
So let's build and run this now and let's see if that fixed our problem we're subtracting the values
so that we can actually get the right printout and look at what we printed out here we have 1 0 0 1
K in the 16 place 0 0 0 1.
That is exactly right.
That's the binary value for one hundred forty five.
That's amazing.
So let's head back here into our view controller get rid of this because this is just test code and
then go back into your binary decimal function and we have one more function to write.
We can now successfully convert an integer into binary.
However we're not yet returning it as the string that we want.
So what we want to do is we want to go ahead and basically take all the values in this array because
remember it's an array of numbers we want to convert each of them to a string and then smushed them
together into one string.
We can display that in our textfield.
So to do this what we're going to do is we're going to go ahead and get rid of that line.
And then below our for loop we're going to go ahead and say let string two int array or sorry string
from int array.
My bad string from into in-tray is going to be equal to our binary rows.
And we're going to use the map function to essentially go through each item and perform an action on
them transforming them into something else.
So type map and then what we can do is we can go ahead and put some curly braces here inside the map
closure and we can just go ahead and say dollar sign zero.
This is a temporary placeholder for each item in this array we're cycling through and whatever dollar
signs zero is that is the value for each item as we cycle through sort of like Roe is two rows.
The dollar sign zero is to map for this array.
So we're going to cast each item as a string and then we can call a cool function called joined and
it's going to smush those all together into one big string so type string and we're going to cast this
value as a string.
Then we can go ahead and return String from an array dot joined.
And what it's going to do is it's going to smush it together into one string.
And that's that's all we got to do.
We're now returning a string of our binary value.
Really really cool stuff.
So now let's write the function that we need for converting a binary value into an integer value.
So let's go ahead and let's say phunk calculate end value for binary.
OK.
Now of course we need to return a string here as well and that's just easier when dealing with textfield
that's how I'm going to do it for this.
All right so here's what we're going to do.
We're going to go ahead and begin with a value animal to player.
We're basically going to go ahead and take our value and we're going to compare it against a multiplier
then every time we're going to double that multiplier so we get to 4 8 632 64 128.
You see where I'm going here.
Let's do it.
You'll understand that when we actually see it in practice so I'm going to start by creating a variable
called value var value equals zero.
Then we're going to create a multiplier var multiplier equals 1.
That's our first multiplier because the first channel is one then we're going to go ahead and use our
bits that are used when we actually create an instance of this class.
So we're going to go ahead and say if let bits equals bits.
Ok that's pretty redundant but basically what we're saying is if Bitts has a value we're going to create
a constant called bits that has that value.
And we're going to go ahead and then use a for loop to cycle through all the bits we passed in.
So type 4 bit in bits.
But the is when we cycle through it it's going to start at the beginning at 128 but we want to start
at 1 and multiply up.
So we're going to go ahead and say it's not reversed.
And then we can go through backwards we can say let new value OK.
Meaning the new value of the bit.
So we're going to start with one.
We're going to say let new value equals bit.
In this case 1 k times the multiplier.
So the new value is 1.
That's the first channel k value is going to be equal to the value which right now is zero.
Plus the new value.
So right now we're at 1.
Then we need to make the multiplier 2.
OK.
So what we're going to say is multiplier equals multiplier times 2 because every time we're multiplying
it by 2.
So let's think about what we're doing after this.
We've gone through the for loop.
We have basically multiplied what we needed and now at the end of the for loop we can say return String
value and what we're doing is we're taking that integer or if we're encapsulating it as a string and
we're sending it back to the function.
So let's go through this one more time.
Oh whoops we need to actually think about this in a different way we're inside of if let let's use guard
let that might be a little easier way to do this.
Guard let bits equals bits else we can return nothing.
That's fine.
Or maybe we can just return error.
That's fine.
OK.
Cool cool.
So get rid of that little curly brace there.
Select all of this and move it back.
And now we can go ahead and actually use everything how we want the error should be gone.
And so let's go through this again so we just went through one.
We know if our values one hundred forty five k we have a value in the ones place we do one times one.
That's the new value and we get one as our value multipliers now too.
So let's go through again the next value is zero.
So the next bit is zero so let new value equals zero times 2 which is equal to zero.
The value is going to be equal to 1 plus 0 K because the current value is 1 because we did have something
in the 1 channel 1 plus 0.
Now the multiplier is 2 times 2 which is 4 for the 4 channel.
So we're going to have a zero here Zero here but then we get to 16 and we have a 1.
So check it out.
Let new value equals one time 16.
So new values equal to 16 value equals 1.
Because remember the value is still 1 plus 16.
Now it's 17 multiplier is four times 2.
Now it's eight.
Do we have anything in eight.
No.
Do we have anything in 16.
No.
How about 32.
No.
Then we get to 128.
We end up with 128 plus 17 which gets us to 145.
So we're basically just looping through multiplying our multiplier by times to anything that's a zero
will stay at zero.
Anything that's a one will become the value of that channel.
That's how it's going to work.
So our our class is now done.
We are able to do all of the math that we need to to convert binary to decimal and decimal to binary.
Let's head over to the next video where we're going to put the two together our UI and this code.
I'll see you there.