# Decimal to Binary conversion

### Question

• I write a program to convert Decimal numbers to Binary numbers. It is working well. But I want to control the bits. i.e -

We can write 31 as a) 11111 or b) 011111. When I run my program it displays 11111. I want to take input bit from a text box and display it like 0011111 or 011111 or what ever bit I input.

How can I do that ??

```Private Sub DecToBin()
Dim i As Integer = txtDec.Text
Dim binary As String = Convert.ToString(i, 2)
txtBin.Text = binary
End Sub```

Saturday, March 06, 2010 8:59 PM

• ```Private Sub DecToBin()
Dim i As Integer = txtDec.Text
Dim binary As String = Convert.ToString(i, 2).PadLeft(8, "0"c)
txtBin.Text = binary
End Sub```

Asgar
• Proposed as answer by Saturday, March 06, 2010 9:20 PM
• Marked as answer by Sunday, March 07, 2010 5:03 AM
Saturday, March 06, 2010 9:10 PM

• You can use String.Split function to break the strig down intoa an array of strings.
http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Then use the convert.tostring to convert each number to binary and place it in the textbox:

'set up a test string

Dim s As String = "1 2 3 4 5 6"
txtDec.Text = S
'
'real code starts here
S = TxtDec.Text
Dim A() As String = s.Split(New Char() {" "}, System.StringSplitOptions.RemoveEmptyEntries)

Dim I As Integer
txtBin.Text = ""
For Each T As String In A
I =
CInt(Val(T))
"0"c) & " "
Next
• Marked as answer by Monday, March 08, 2010 6:49 AM
Monday, March 08, 2010 1:18 AM

### All replies

Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
Saturday, March 06, 2010 9:01 PM
• ```Private Sub DecToBin()
Dim i As Integer = txtDec.Text
Dim binary As String = Convert.ToString(i, 2).PadLeft(8, "0"c)
txtBin.Text = binary
End Sub```

Asgar
• Proposed as answer by Saturday, March 06, 2010 9:20 PM
• Marked as answer by Sunday, March 07, 2010 5:03 AM
Saturday, March 06, 2010 9:10 PM
• Hi is it readable now ??

Yes thank you . The reason for this is first readability and second if the code where more complex and a member wished to copy and paste your code in order to debug it if it is not formatted the member may not bother with it because it will take time to reformat it in the IDE in order to begin debugging it . You will not get as much help as you might have gotten if the code had been formatted .

Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
Saturday, March 06, 2010 9:25 PM
• Hi all

I need some more help. I want to take more than one number separated by one space from txtDec textbox and display their binary form again separated by one space in txtBin textbox.

How can I do that ???
Sunday, March 07, 2010 8:15 PM

• You can use String.Split function to break the strig down intoa an array of strings.
http://msdn.microsoft.com/en-us/library/system.string.split.aspx

Then use the convert.tostring to convert each number to binary and place it in the textbox:

'set up a test string

Dim s As String = "1 2 3 4 5 6"
txtDec.Text = S
'
'real code starts here
S = TxtDec.Text
Dim A() As String = s.Split(New Char() {" "}, System.StringSplitOptions.RemoveEmptyEntries)

Dim I As Integer
txtBin.Text = ""
For Each T As String In A
I =
CInt(Val(T))