473,416 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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

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
13 9182

"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
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
"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
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
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
"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
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

"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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kyiu | last post by:
I am using the VBScript and WSH to get the existing printer network. The code: <script language="VBScript" type="text/VBScript"> Dim existing Set WshNetwork = CreateObject("WScript.Network") ...
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
2
by: Tiraman :-\) | last post by:
Hi Everyone, i have the following problem in my client-server Application My server take array and serialize it into the memorystream Dim ns As NetworkStream = client.GetStream() Dim writer...
13
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
2
by: RC | last post by:
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_sort2 You can see above link or read below i copy/paste from above link <script type="text/javascript"> function sortNumber(a, b)...
1
by: DukeRock | last post by:
This is the function I'm getting the error on: S32 GuiAviBitmapCtrl::fileOpen() { S32 rval; if (!dStrcmp(mAviFilename,"")) return MOVERR_NOVIDEOSTREAM; rval =...
10
by: raghu | last post by:
// Prog. to convert integer to binary #include<stdio.h> int main(void) { int i=4; int j; for(j=0;j<3;j++) { i=i%2;
5
by: rubelbd | last post by:
I have face the problem When download .doc file this is corrupted Please Help me. my code is : String FileName = Request.QueryString; StreamReader sr = new...
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.