Basicly I'm trying to make a 1 lined binary converter. Ill send you guys some screenshots if you want to help, I'm not begging for you guys to do it. I saw on the dash it said you guys would be happy to help.
If (tb1.Text) * (CInt(tb2.Text) + (tb3.Text)) * (CInt(tb3.Text) + (tb4.Text)) * (CInt(tb4.Text)) Then Label6.Text
Are we trying to convert binary to decimal or decimal to binary here?
If it's binary to decimal just iterate through all of the textbox's, if the user has inputed a '1' then add the corresponding number to a running total, if they have entered a '0' then skip on to the next one.
In that case take a look through my previous post.
You can also make good use of the .tag property in each textbox. If you put the number that they represent in the tag, when it comes to adding on the numbers, it will be a lot easier.
Essentially what you need to do is go through each textbox, look at the .Text property, if it's a "1", then add the number of the .tag to a running total variable. Don't do anything if the .Text property is not a "1". Then after you have visited all the textboxes, display the running total variable in the result label.
Well you might be able to do it with one line of code but its not always a good thing to do. It reduces the readability of your code and makes it a lot harder for someone else to figure out what it's meant to do.
runningTotal += 8 is the same as runningTotal = runningTotal + 8
Why do you have different text boxes for each digit in the binary number? You can't do this with 1 textbox?
Binary IS a number, it's just the base 2 representation of a number. We commonly write numbers in base 10, because that's what we humans are comfortable reading numbers in. It is postulated that it derives from the fact we have 10 fingers and 10 toes... because we tend to count. by. fingers.
And yes a conversion can be done in one line if you have the full string that user typed in. Either use ONE textbox, or combine the text in all text boxes. Then you parse the value, parsing is the act of reading a string and interpretting it as a non-string value. In this case you are reading a numeric value written in binary, and parsing it to a usable numeric value. A function exists for doing this and it's Convert.ToInt32(), one of its overloads supports base specification for the common bases (2/binary, 8/octal, 10/decimal, 16/hexadecimal).
It's also supported for conversions to all the other numeric types like byte, long, double, etc if need be. But Int32 is the one you probably care about right now.
THis of course just does the conversion for you, you may want to research about base conversion so you can understand that there is a big difference between the written out "5398" and the actual value of five thousand three hundred ninety eight. Think of it this way, see how I just wrote that number 2 different ways yet they're equal... well that's what binary is as well. Just another way to write it.
This post has been edited by lordofduct: 01 November 2011 - 10:04 AM
You want to concatenate the values in the text boxes so you end up with a full binary string, then run that through the convert function, with the 2 as the 2nd parameter to tell it you're passing a binary number to it.
See, it HAS to be done using my original code, that means using multiplication and addition. I showed my instructor the code and he said it was a "cheap and easy solution". He said I would fail the project if I didn't use the original code.