00:01
This question asks us to give a recursive method that parses binary number as a string into a decimal integer.
00:08
And basically our header is going to be public static int into decimal, and then we give a string input, which is binary string.
00:29
And we want to enter a binary string and display the decimal equivalent.
00:37
So to convert to decimal given a string, like a binary string, simply what we're going to do is we're going to have to call binary to decimal on itself over and over again.
00:53
Right.
00:58
So the inside of the method over here, the first thing we're going to do, the first thing we're going to do here is we're going to get the land.
01:14
So the base case is the first thing we do right.
01:16
So if binary string dot length equals zero, then we're going to return zero.
01:34
So that's the first part.
01:35
That's a base case.
01:36
Because we're going to slowly start decreasing the length of the string.
01:41
So then what we're going to do is we're going to get substrings by getting essentially the first one and the second.
01:50
We're going to get essentially isolate each single one of, the terms.
01:56
So we're going to have string for current and that's going to be binary string dot substring and we're going to take a substring from zero to one.
02:11
Then we're going to have another string and this one will be after, so current after.
02:20
This will be a binary string dot substring and we'll give it the starting index of one and it's going to take the entire rest of the string.
02:30
And then now that we have this, we want to get the integer value of our current thing, right? multiply it times the power of 2 to the power of whatever the length is minus 1.
02:50
So if it's 0, then it's not going to work.
02:52
If it's 1, it is going to work.
02:54
So essentially what we're doing here is we're getting the first character.
03:03
Converting it to an integer and then if that integer is zero it's going to add if it's one it's not going to add i'm sorry other way around if it's zero it's not going to add and if it's one it is going to add to sum and then we're going to recursively call binary bin to decimal after so what we could do here is instead of having a string we could have an integer current value and the string remainder string remaining string whatever you could just cost they remain and current value is essentially going to equal to binary string dot car at zero dot um well it's going to be the car at zero and then um sorry we're going to get the character at zero and we have to convert it as well to and to convert it to an integer we're going to use integer.
04:04
Dot parsint of binary string dot car at zero so it's a little bit of a lot but attempts to what we essentially did is we're getting the current value at the character at the current value the character zero and we're getting the integer value of it so that now when we write the return statement we could simply say return current valve times so now you can see where this really gets comes into play right so we have a number called zero and zero integer value at zero is one right so when we do one times and then we'll have integer value map that power to to the binary string that length minus one so what this does is so the length minus one length is three minus one is is going to give us 2.
05:18
So it's going to say 1 times 2 to the 1.
05:22
Sorry, 1 times 2 to the 2, which will give you 4.
05:27
And then we're going to return by adding that to binary, sorry, bin to decimal of remain...