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

J

Jason

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.
 
J

james

Jason said:
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
 
J

Jason

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 said:
Jason said:
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
 
H

Herfried K. Wagner [MVP]

Jason said:
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))
///
 
J

Jay B. Harlow [MVP - Outlook]

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


| 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.
|
|
 
J

james

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
 
H

Herfried K. Wagner [MVP]

james said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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
| 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
|
|
message | > 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
| >
| >
| > | > | 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.
| > |
| > |
| >
| >
|
|
 
J

james

Herfried K. Wagner said:
Add 'Imports System.Net' as import instead.

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
 
J

james

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 said:
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
| 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
|
|
message | > 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
| >
| >
| > | > | 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.
| > |
| > |
| >
| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

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/d...ml/frlrfSystemNetIPAddressClassParseTopic.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
| 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>>
 
J

james

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
 
J

Jason

how about if I want to go from decimal or binary to Hex?

How would I acomplish that?


james said:
Jason said:
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
 
H

Herfried K. Wagner [MVP]

Jason said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top