# Visual Basic Binary and Back Converter (Now with Hex!)

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 (or integer) and vice-versa. Now, I have added a Hexadecimal section for anyone needing to see the binary in Hex! Same limitations of 255 is the maximum decimal allowed at the moment but you can easily change that by modifying the powers variables.

Notes: This is a single byte conversion with 8 bits so the integer ends at 255. This can be extended but  I wanted to make it simple to understand.

# 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 use a 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. Integer 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. Binary 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 ```

### A. Summary

The code below changes the binary code to hexadecimal and it has the same requirement that it must be 8 characters long (also called bits when you deal with them directly). As you can see in the first for loop, I step 4 so I can have 4 bits of the binary code at a time for converting to hexadecimal. The code adds up this 4 bits to produce an decimal or integer value. Which is then converted to Hexadecimal using the Hex function.

```Dim tempstr As String = TxtBinary.Text ```
`       ````Dim powers() As Integer = New Integer(7) {128, 64, 32, 16, 8, 4, 2, 1} ```
`       ````Dim powers2() As Integer = New Integer(3) {8, 4, 2, 1} ```
`       ````Dim hexvalues() As String = New String(14) {"1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"} ```
`       ````Dim answer2 As String = "" ```
`       ````Dim tmpanswer2 As Integer ```
`       ````Dim tmpbinary As List(Of String) = New List(Of String) ```
`       ````Dim values(7) As Integer ```
`       ````If TxtBinary.Text.Length >= 8 And tempstr <> "" Then 'only covert to hexidecimal if tempstr is not equal to nothing ```
`           ````For i = 0 To tempstr.Length - 1 Step 4 'add only 4 bits at a time of data ```
`               ````If i = tempstr.Length - 1 Then ```
`                   ````tmpbinary.Add(tempstr.Substring(i, 1)) ```
`               ````Else ```
`                   ````tmpbinary.Add(tempstr.Substring(i, 4)) ```
`               ````End If ```
`           ````Next ```
`           ````For i = 0 To tmpbinary.Count - 1 ```
`               ````'MsgBox(tmpbinary(i)) ```
`               ````For j = 0 To 3 ```
`                   ````values(j) = CInt(tmpbinary(i).Substring(j, 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(j) = 1 Then ```
`                       ````values(j) = powers2(j) ```
`                   ````Else ```
`                   ````End If ```
`                   ````tmpanswer2 = tmpanswer2 + values(j) ```
`               ````Next ```
`               ````'  MsgBox(tmpanswer2.ToString) ```
`               ````answer2 = answer2 + Hex(tmpanswer2) ```
`               ````tmpanswer2 = 0 ```
`           ````Next i ```
`       ``End If`

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. People become confused when they are in stressful situation's. 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.