a little VB.NET help for a beginner? EDIT: so close

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
hey-

I just started using VB.NET and I'm trying to write a program for a Computer Architecture class that converts a binary number into a decimal the other way around. I've got the form set up like I want it...just two text boxes, labels, and a convert button. I'm having trouble with the logic on the button though, as well as the formula. Here's what I have so far:


Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
txtBin.Text = " "
txtDec.Text = " "

If (txtBin.Text) Then
txtDec.Text = (conversion formula)
ElseIf (txtDec.Text) Then
txtBin.Text = (conversion formula)
End If

End Sub

Private Sub txtBin_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBin.TextChanged

End Sub
End Class

I'm getting errors on the if statements about using a string as a boolean value. All I want to do is have it perform the conversion if there's text in the box. I'm not looking to make a production quality program...this is just a homework assignment for the arch class...not my VB. Any help would be appreciated, thanks.
 

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
Originally posted by: flamingsouls
I would try CType ing the text to a number. That's a start.


Thanks
Josh

Ok, the logic errors are taken care of...now all I need is a formula to convert from decimal to binary and back.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
You should be able to convert the text into an int with Int32.Parse, and then print out the number in any base you want with String.Format. BTW, in your original code, you should be comparing the ".Text" property with something like "" or String.Empty to see if it is blank. An empty string is not a boolean, and that's why it was giving you compilation errors before.
 

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

If (txtBin.Text <> String.Empty) Then
Dim temp As String
temp = txtBin.Text
Int32.Parse(temp)
String.Format(temp, 10)
txtDec.Text = temp

ElseIf (txtDec.Text <> String.Empty) Then
Dim temp2 As String
temp2 = txtDec.Text
Int32.Parse(temp2)
String.Format(temp2, 2)
txtBin.Text = temp2

End If

End Sub

Alright, this is what I have now. When the program runs, it just spits out the number that you put in one box into the other...any ideas?
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
My apologies. I could have sworn that Format and Parse had formatting codes available to translate between decimal and binary. There are certainly codes available for hex. There may still be ways to represent the integer as bytes and shift bits off one at a time until you've read out the binary form. Anyway, the most general way to do this is like this:

If you're converting an integer from decimal to binary, you know that the number is at most 32 bits. Do something like this (in pseudocode, not VB)

integer temp = decimal_number
string result = ""
loop values of i from 32 to 0, decrementing by 1
integer power_of_two = 2^i
if (temp > power_of_two)
{
temp = temp - power_of_two
result = result + "1"
}
else
{
result = result + "0"
}
end loop

This pseudocode recognizes that when you convert something to binary, you are trying to figure out which powers of two make up the number. For instance, the number 9 is composed of 2^3 + 2^0 (or 8+1). In the pseudocode, we would spit out a zero for every power of two until i reaches 3. then we would spit out a 1, then two more zeros, then a 1. So we should get: 0000 0000 0000 0000 0000 0000 0000 1001. I put in the spaces for readability.

If you wanted to convert from binary to decimal, you should read the characters one at a time, and for every character that is a 1, add the corresponding power of 2. The right most binary digit corresponds to 2^0. The one to the left of that corresponds to 2^1, etc. Add up all the powers of two, and you should have your integer again.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
that loop should probably go from 31 to 0. also, you need to account for negatives. have you covered binary representation of negative numbers yet?
 

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
haven't worked on it since the homework was due this morning...it's turned into a personal vendetta type problem, so I'll probably take a look at it later tonight.
 

minus1972

Platinum Member
Oct 4, 2000
2,245
0
0
prof came through with some code to help me out...here's the revised code:

Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click

If (txtBin.Text <> String.Empty) Then
Dim output As String
Dim tempInt As Byte
tempInt = txtBin.Text
output = Convert.ToString(tempInt, 10)
txtDec.Text = output

ElseIf (txtDec.Text <> String.Empty) Then
Dim output2 As String
Dim tempByte As Integer
tempByte = txtDec.Text
output2 = Convert.ToString(tempByte, 2)
txtBin.Text = output2

End If

End Sub

now I'm converting from decimal to binary with no problem but binary to decimal gives me a buffer overflow error...so close.