By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,180 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,180 IT Pros & Developers. It's quick & easy.

How to write a VB prog to convert decimal to binary?? Please help.

P: n/a
Could someone here show me how I would write a vb program to convert decimal
ip address to binary?

For example a small form with a convert button and a label for the result
and a textbox for the ip.

So I would want 11000000 10101000 00000010 00001010 tp show up inthe
label if I entered 192.168.2.10 into the text box.

I have no idea even how to begin this, any help would be great.
Nov 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

"Jason" <no**@none.invalid> wrote in message news:e9**************@TK2MSFTNGP12.phx.gbl...
Could someone here show me how I would write a vb program to convert decimal ip address to binary?

For example a small form with a convert button and a label for the result and a textbox for the ip.

So I would want 11000000 10101000 00000010 00001010 tp show up inthe label if I entered 192.168.2.10 into the text box.

I have no idea even how to begin this, any help would be great.

I don't remember exactly where I got this, but, see if this doesn't help get you going:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox1.Text Like "*[!0-9]*" Then

TextBox1.Text = ""

Exit Sub

ElseIf TextBox1.Text = "" Then

Exit Sub

Else

End If

'Converts numbers to BINARY numbers 0001 0010 0100 etc.

Dim from As Int32

If IsNumeric(TextBox1.Text) Then

from = TextBox1.Text

Dim n As Int16

Do

n = from Mod 2

TextBox2.Text = n & TextBox2.Text

from \= 2

Loop Until from = 0

End If

End Sub

This will only accept numbers and will clear the first textbox (Textbox1) if it contains anything other than a number. At least
it will give you the general idea. You will need to tweak it to work exactly the way you want.
james

Nov 23 '05 #2

P: n/a
anyway that I can keep all of the 0's with this code?

if I enter 192.168.1.1 - the .1.1 come up as just 1 without the leading
7 0's like 00000001

"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:uq*************@TK2MSFTNGP09.phx.gbl...

"Jason" <no**@none.invalid> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
Could someone here show me how I would write a vb program to convert
decimal ip address to binary?

For example a small form with a convert button and a label for the result
and a textbox for the ip.

So I would want 11000000 10101000 00000010 00001010 tp show up inthe
label if I entered 192.168.2.10 into the text box.

I have no idea even how to begin this, any help would be great.

I don't remember exactly where I got this, but, see if this doesn't help
get you going:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If TextBox1.Text Like "*[!0-9]*" Then

TextBox1.Text = ""

Exit Sub

ElseIf TextBox1.Text = "" Then

Exit Sub

Else

End If

'Converts numbers to BINARY numbers 0001 0010 0100 etc.

Dim from As Int32

If IsNumeric(TextBox1.Text) Then

from = TextBox1.Text

Dim n As Int16

Do

n = from Mod 2

TextBox2.Text = n & TextBox2.Text

from \= 2

Loop Until from = 0

End If

End Sub

This will only accept numbers and will clear the first textbox (Textbox1)
if it contains anything other than a number. At least it will give you
the general idea. You will need to tweak it to work exactly the way you
want.
james

Nov 23 '05 #3

P: n/a
"Jason" <no**@none.invalid> schrieb:
Could someone here show me how I would write a vb program to convert
decimal ip address to binary?

For example a small form with a convert button and a label for the result
and a textbox for the ip.

So I would want 11000000 10101000 00000010 00001010 tp show up inthe
label if I entered 192.168.2.10 into the text box.


Quick and dirty:

\\\
Dim Text As String
Dim Parts() As String = Split("192.168.2.10", ".")
For Each Part As String In Parts
Text &= Convert.ToString(CInt(Part), 2).PadLeft(8, "0") & " "
Next Part
MsgBox(RTrim(Text))
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #4

P: n/a
Jason,
In addition to the other comments, I would recommend using
System.Net.IPAddress to hold & parse the IP address itself.

Then you can use IPAddress.GetAddressBytes to get the individual bytes of
the address allowing your routine to work for both IP4 (192.168.2.10) and
IP6 (0:0:0:0:0:0:0:1) addresses.

Something like:

Dim address As IPAddress

address = IPAddress.Parse("192.168.2.10") ' sample IP4
address = IPAddress.Parse("0:0:0:0:0:0:0:1") ' sample IP6

Dim bytes() As Byte = address.GetAddressBytes()
Dim value As New System.Text.StringBuilder()
For index As Integer = 0 To bytes.Length - 1
value.Append(Convert.ToString(bytes(index), 2).PadLeft(8, "0"c))
value.Append(" "c)
Next
value.Length -= 1 ' trim trailing space
Debug.WriteLine(address.ToString())
Debug.WriteLine(value.ToString())
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jason" <no**@none.invalid> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
| Could someone here show me how I would write a vb program to convert
decimal
| ip address to binary?
|
| For example a small form with a convert button and a label for the result
| and a textbox for the ip.
|
| So I would want 11000000 10101000 00000010 00001010 tp show up inthe
| label if I entered 192.168.2.10 into the text box.
|
| I have no idea even how to begin this, any help would be great.
|
|
Nov 23 '05 #5

P: n/a
Jay, I just tried your example and I get an error on the line: Dim address as IPAddress and the tooltip shows
Type Expected ). Not sure what that means, if I add a decimal after the IPAddress the dropdown only shows Form1. I added:
Imports System.Net.IPAddress at the top and it still does that.
Just thought I would let you know.
james
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Jason,
In addition to the other comments, I would recommend using
System.Net.IPAddress to hold & parse the IP address itself.

Then you can use IPAddress.GetAddressBytes to get the individual bytes of
the address allowing your routine to work for both IP4 (192.168.2.10) and
IP6 (0:0:0:0:0:0:0:1) addresses.

Something like:

Dim address As IPAddress

address = IPAddress.Parse("192.168.2.10") ' sample IP4
address = IPAddress.Parse("0:0:0:0:0:0:0:1") ' sample IP6

Dim bytes() As Byte = address.GetAddressBytes()
Dim value As New System.Text.StringBuilder()
For index As Integer = 0 To bytes.Length - 1
value.Append(Convert.ToString(bytes(index), 2).PadLeft(8, "0"c))
value.Append(" "c)
Next
value.Length -= 1 ' trim trailing space
Debug.WriteLine(address.ToString())
Debug.WriteLine(value.ToString())
--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jason" <no**@none.invalid> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
| Could someone here show me how I would write a vb program to convert
decimal
| ip address to binary?
|
| For example a small form with a convert button and a label for the result
| and a textbox for the ip.
|
| So I would want 11000000 10101000 00000010 00001010 tp show up inthe
| label if I entered 192.168.2.10 into the text box.
|
| I have no idea even how to begin this, any help would be great.
|
|

Nov 23 '05 #6

P: n/a
"james" <jjames700ReMoVeMe at earthlink dot net> schrieb:
Jay, I just tried your example and I get an error on the line: Dim address
as IPAddress and the tooltip shows
Type Expected ). Not sure what that means, if I add a decimal after the
IPAddress the dropdown only shows Form1. I added: Imports
System.Net.IPAddress at the top and it still does that.


Add 'Imports System.Net' as import instead.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #7

P: n/a
James,
As Herfried suggests: add "Imports System.Net" at the top of the module. Or
simply use:

Dim address As System.Net.IPAddress

If it still errors, verify you have you have a reference (Project -
Properties - References) to the System.dll assembly. VB normally always
references the System.dll so that should not be a problem.

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
| Jay, I just tried your example and I get an error on the line: Dim address
as IPAddress and the tooltip shows
| Type Expected ). Not sure what that means, if I add a decimal after the
IPAddress the dropdown only shows Form1. I added:
| Imports System.Net.IPAddress at the top and it still does that.
| Just thought I would let you know.
| james
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
| > Jason,
| > In addition to the other comments, I would recommend using
| > System.Net.IPAddress to hold & parse the IP address itself.
| >
| > Then you can use IPAddress.GetAddressBytes to get the individual bytes
of
| > the address allowing your routine to work for both IP4 (192.168.2.10)
and
| > IP6 (0:0:0:0:0:0:0:1) addresses.
| >
| > Something like:
| >
| > Dim address As IPAddress
| >
| > address = IPAddress.Parse("192.168.2.10") ' sample IP4
| > address = IPAddress.Parse("0:0:0:0:0:0:0:1") ' sample IP6
| >
| > Dim bytes() As Byte = address.GetAddressBytes()
| > Dim value As New System.Text.StringBuilder()
| > For index As Integer = 0 To bytes.Length - 1
| > value.Append(Convert.ToString(bytes(index), 2).PadLeft(8,
"0"c))
| > value.Append(" "c)
| > Next
| > value.Length -= 1 ' trim trailing space
| > Debug.WriteLine(address.ToString())
| > Debug.WriteLine(value.ToString())
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Jason" <no**@none.invalid> wrote in message
| > news:e9**************@TK2MSFTNGP12.phx.gbl...
| > | Could someone here show me how I would write a vb program to convert
| > decimal
| > | ip address to binary?
| > |
| > | For example a small form with a convert button and a label for the
result
| > | and a textbox for the ip.
| > |
| > | So I would want 11000000 10101000 00000010 00001010 tp show up
inthe
| > | label if I entered 192.168.2.10 into the text box.
| > |
| > | I have no idea even how to begin this, any help would be great.
| > |
| > |
| >
| >
|
|
Nov 23 '05 #8

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:OR*************@TK2MSFTNGP15.phx.gbl...
"james" <jjames700ReMoVeMe at earthlink dot net> schrieb:
Jay, I just tried your example and I get an error on the line: Dim address as IPAddress and the tooltip shows
Type Expected ). Not sure what that means, if I add a decimal after the IPAddress the dropdown only shows Form1. I added:
Imports System.Net.IPAddress at the top and it still does that.


Add 'Imports System.Net' as import instead.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Still get the same error asking for : Type Expected) on the same line as before. Using Visual Studio 2003. I am not sure what is
happening. If I change "Imports System.Net" to: "Import System.Net", I get an error too. There must be something missing on my
system for this to not work.
james


Nov 23 '05 #9

P: n/a
That (adding reference to System.dll) and changing : address = IPAddress.Parse("192.168.2.10")
to: address = address.Parse("192.168.2.10")
fixed the problem. the second IP6 address throws an error. But, commenting it out gets rid of it.
Anyway, I didn't mean to hijack this part of the thread, but, I found the idea interesting and wanted to
be sure I understood it correctly.
As always Jay, you really do know your stuff!!!
james
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in message news:eZ**************@TK2MSFTNGP10.phx.gbl...
James,
As Herfried suggests: add "Imports System.Net" at the top of the module. Or
simply use:

Dim address As System.Net.IPAddress

If it still errors, verify you have you have a reference (Project -
Properties - References) to the System.dll assembly. VB normally always
references the System.dll so that should not be a problem.

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:OO**************@tk2msftngp13.phx.gbl...
| Jay, I just tried your example and I get an error on the line: Dim address
as IPAddress and the tooltip shows
| Type Expected ). Not sure what that means, if I add a decimal after the
IPAddress the dropdown only shows Form1. I added:
| Imports System.Net.IPAddress at the top and it still does that.
| Just thought I would let you know.
| james
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
| > Jason,
| > In addition to the other comments, I would recommend using
| > System.Net.IPAddress to hold & parse the IP address itself.
| >
| > Then you can use IPAddress.GetAddressBytes to get the individual bytes
of
| > the address allowing your routine to work for both IP4 (192.168.2.10)
and
| > IP6 (0:0:0:0:0:0:0:1) addresses.
| >
| > Something like:
| >
| > Dim address As IPAddress
| >
| > address = IPAddress.Parse("192.168.2.10") ' sample IP4
| > address = IPAddress.Parse("0:0:0:0:0:0:0:1") ' sample IP6
| >
| > Dim bytes() As Byte = address.GetAddressBytes()
| > Dim value As New System.Text.StringBuilder()
| > For index As Integer = 0 To bytes.Length - 1
| > value.Append(Convert.ToString(bytes(index), 2).PadLeft(8,
"0"c))
| > value.Append(" "c)
| > Next
| > value.Length -= 1 ' trim trailing space
| > Debug.WriteLine(address.ToString())
| > Debug.WriteLine(value.ToString())
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Jason" <no**@none.invalid> wrote in message
| > news:e9**************@TK2MSFTNGP12.phx.gbl...
| > | Could someone here show me how I would write a vb program to convert
| > decimal
| > | ip address to binary?
| > |
| > | For example a small form with a convert button and a label for the
result
| > | and a textbox for the ip.
| > |
| > | So I would want 11000000 10101000 00000010 00001010 tp show up
inthe
| > | label if I entered 192.168.2.10 into the text box.
| > |
| > | I have no idea even how to begin this, any help would be great.
| > |
| > |
| >
| >
|
|

Nov 23 '05 #10

P: n/a
James,
You need .NET 1.1 or higher for IP6 support.

Interesting VB 2003 (.NET 1.1) throws an exception on the IP6 address,
although I took the sample from from .NET 1.1's MSDN page...

http://msdn.microsoft.com/library/de...ParseTopic.asp

The IP6 address I gave works in VB 2005 (.NET 2.0).

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:em*************@TK2MSFTNGP09.phx.gbl...
| That (adding reference to System.dll) and changing : address =
IPAddress.Parse("192.168.2.10")
| to: address = address.Parse("192.168.2.10")
| fixed the problem. the second IP6 address throws an error. But,
commenting it out gets rid of it.
| Anyway, I didn't mean to hijack this part of the thread, but, I found the
idea interesting and wanted to
| be sure I understood it correctly.
| As always Jay, you really do know your stuff!!!
| james
|
|
<<snip>>
Nov 23 '05 #11

P: n/a
Jay, that is the version I am using. I guess there is a bug in 2003 that is causing the problem. But, at least it seems to be
fixed in 2005. Cannot wait to get my copy.
james

"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in message news:eQ**************@TK2MSFTNGP10.phx.gbl...
James,
You need .NET 1.1 or higher for IP6 support.

Interesting VB 2003 (.NET 1.1) throws an exception on the IP6 address,
although I took the sample from from .NET 1.1's MSDN page...

http://msdn.microsoft.com/library/de...ParseTopic.asp

The IP6 address I gave works in VB 2005 (.NET 2.0).

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

Nov 23 '05 #12

P: n/a
how about if I want to go from decimal or binary to Hex?

How would I acomplish that?
"james" <jjames700ReMoVeMe at earthlink dot net> wrote in message
news:uq*************@TK2MSFTNGP09.phx.gbl...

"Jason" <no**@none.invalid> wrote in message
news:e9**************@TK2MSFTNGP12.phx.gbl...
Could someone here show me how I would write a vb program to convert
decimal ip address to binary?

For example a small form with a convert button and a label for the result
and a textbox for the ip.

So I would want 11000000 10101000 00000010 00001010 tp show up inthe
label if I entered 192.168.2.10 into the text box.

I have no idea even how to begin this, any help would be great.

I don't remember exactly where I got this, but, see if this doesn't help
get you going:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If TextBox1.Text Like "*[!0-9]*" Then

TextBox1.Text = ""

Exit Sub

ElseIf TextBox1.Text = "" Then

Exit Sub

Else

End If

'Converts numbers to BINARY numbers 0001 0010 0100 etc.

Dim from As Int32

If IsNumeric(TextBox1.Text) Then

from = TextBox1.Text

Dim n As Int16

Do

n = from Mod 2

TextBox2.Text = n & TextBox2.Text

from \= 2

Loop Until from = 0

End If

End Sub

This will only accept numbers and will clear the first textbox (Textbox1)
if it contains anything other than a number. At least it will give you
the general idea. You will need to tweak it to work exactly the way you
want.
james

Nov 23 '05 #13

P: n/a
"Jason" <no**@none.invalid> schrieb:
how about if I want to go from decimal or binary to Hex?


\\\
Dim s As String
s = Convert.ToString(22, 2)
MsgBox(s)
s = Convert.ToString(Convert.ToInt32(s, 2), 16)
MsgBox(s)
///

In addition to 'Convert.ToString' and 'Convert.ToInt32' VB provides 'Hex'
and 'Val' functions which can cope with decimal, hexadecimal and octal
values.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.