Resources For IT Professionals

# Visual Basic Binary and Back Converter

I have decided to write a binary to decimal and decimal to binary converter because of the demand from Visual Basic users having asked questions or talked about it on the Visual Basic forums. I decided to make the code easy to understand to show that binary is not hard to do in Visual Basic. In fact this took about all of an hour to throw together and it does have basic error checking (could add more error checking later to improve it if someone wishes).  The code mentioned below will convert from binary to decimal and vice-vera.

Notes: This is a single byte conversion with 8 bits so the integer ends at 255. This can be extended but as stated above I wanted to make it simple to understand so it could be expanded upon later if need be. I updated the code below to reflect this change. I will upload an updated source code copy today while I am thinking about it.

# What must be done?

## 1. Integer to Binary code

### A. Summary

In the below code for converting decimal to binary I do a countdown of the numbers 128,64,32,16,8,4,2, and 1 to determine the binary numbers that will be used. I subtracted from each number that is lower then the number given in the textbox until the textbox number equals zero.  It skips over the numbers in powers array that are less then the textbox number. For instance, put 128 into the textbox and it can only subtract from 128 so you should get 10000000 in the binary textbox or 128-128 = 0. Another instance is suppose you have 129 you should get 10000001 in binary. As you can see from the code binaryplaceholder holds the binary version of the decimal number until it get put into the binary textbox. As you can see mathplaceholder counts down through the number from decimal to binary. mathplaceholder gets counted down until the number inside equals zero and then everything subtracted after that equals zero and cannot be subtracted from so its 0 in binary.  Please look at Figure 1 at the table for a better idea of the conversion process I am talking about.

### B. Code

`If` `CInt````(TxtDecimal.Text) <= 255 ````Then`
`           ``If` `IsNumeric(TxtDecimal.Text) = ``True` `Then`
` `
` `
`               ``Dim` `powers() ``As` `Integer` ``` = ````New` ``` Integer````(7) {128, 64, 32, 16, 8, 4, 2, 1}`
`               ``Dim` `binaryplaceholder ``As` `String` ``` = ````""`
`               ``If` `TxtDecimal.Text = ``""` `Then`
`                   ``Exit` `Sub`
`               ``End` `If`
`               ``Dim` `mathplaceholder ``As` `Integer`
`               ````mathplaceholder = ````CInt``(TxtDecimal.Text)`
`               ``Dim` `answer ``As` `Integer`
` `
`               ``For` `i = 0 ``To` `7`
` `
`                   ````'if the number given to convert to binary is large enough to minus from then do it and put the result in answer variable```
`                   ``If` `mathplaceholder >= powers(i) ``Then`
`                       ````binaryplaceholder = binaryplaceholder + ````CStr``(1)`
`                       ``answer = mathplaceholder - powers(i)`
`                       ``mathplaceholder = answer`
`                   ``Else`
`                       ````binaryplaceholder = binaryplaceholder + ````CStr``(0)`
`                   ``End` `If`
` `
` `
`               ``Next` `i`
`               ``TxtBinary.Text = binaryplaceholder.ToString`
`           ``Else`
`               ``Exit` `Sub`
`           ``End` `If`
`       ``End` `If`

Figure  1 - math for number 129 in binary

 Math for binary conversion 128 64 32 16 8 4 2 1 129 - 128 = 1 1 0 0 0 0 0 0 1 1 cannot be subtracted evenly from 64 so its 0 (1-64 = -63) 1 cannot be subtracted evenly from 32 so its 0 (1-32 = -31) 1 cannot be subtracted evenly from 16 so its 0 (1-16 = -15) 1 cannot be subtracted evenly from 8 so its 0 (1-8 = -7) 1 cannot be subtracted evenly from 4 so its 0 (1-4 = -3) 1 cannot be subtracted evenly from 2 so its 0 (1-2 = -1) 1-1 = 0

## 2. Binary to decimal code

### A. Summary

The binary code does this vice-versa it counts up the 1's and 0's and assigns the correct number from powers2 array position. The catch is before I do the conversion I must have 8 characters and it will not convert less then 8 characters in the binary textbox.

### B. Code

``` Dim powers2() As Integer = New Integer(7) {128, 64, 32, 16, 8, 4, 2, 1}    Dim binaryplaceholder2 As String = ""    If TxtBinary.Text = "" Then        Exit Sub    End If    Dim values(7) As Integer    Dim strvalues(7) As String    Dim answer As Integer = 0    '  MsgBox("length of textbox:" + TxtBinary.Text.Length.ToString)    For i = 0 To 7        If TxtBinary.Text.Length >= 0 And TxtBinary.Text.Length <= 7 Then            Exit Sub        End If        values(i) = CInt(TxtBinary.Text.Substring(i, 1))           'if the number given to convert to binary is large enough to minus from then do it and put the result in answer variable        If values(i) = 1 Then            values(i) = powers2(i)        Else        End If        answer = answer + values(i)          Next i    TxtDecimal.Text = answer.ToString ```

Conclusion

I think this will help when answering questions on MSDN forums about converting to binary without getting technical and off-topic for such a simple topic.  Later I might add a hexadecimal section to this article but for now I will upload what I have. I do not have my hexadecimal documentation from my previous cisco ccna class with me but when I can get to it I will update this article.  Binary reminds me of this scene from back to the future.

I do not know if anyone is old enough to have seen it. When it hits someone they end up needing to understand it bad but in the end it works out.