IPAddress.Parse Method
Converts an IP address string to an IPAddress instance.
Assembly: System (in System.dll)
Parameters
- ipString
- Type: System.String
A string that contains an IP address in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.
| Exception | Condition |
|---|---|
| ArgumentException |
ipString can't contain Unicode characters. |
| ArgumentNullException |
ipString is null. |
| FormatException |
ipString is not a valid IP address. |
The static Parse method creates an IPAddress instance from an IP address expressed in dotted-quad notation for IPv4 and in colon-hexadecimal notation for IPv6.
The Parse method accepts octal, decimal, and hexadecimal number formats in the ipString parameter for an IPv4 address, even when mixed. The Parse method can't handle Unicode characters in the ipString parameter.
The number of parts (each part is separated by a period) in ipString determines how the IP address is constructed. A one part address is stored directly in the network address. A two part address, convenient for specifying a class A address, puts the leading part in the first byte and the trailing part in the right-most three bytes of the network address. A three part address, convenient for specifying a class B address, puts the first part in the first byte, the second part in the second byte, and the final part in the right-most two bytes of the network address. For example:
|
Number of parts and example ipString |
IPv4 address for IPAddress |
|---|---|
|
1 -- "65536" |
0.0.255.255 |
|
2 -- "20.2" |
20.0.0.2 |
|
2 -- "20.65535" |
20.0.255.255 |
|
3 -- "128.1.2" |
128.1.0.2 |
|
4 -- "0xC0.0xA8.0x1.0x01" |
192.168.1.1 |
|
4 -- "0300.0250.01.01" |
192.168.1.1 |
|
4 -- "0300.0xA8.1.1" |
192.168.1.1 |
The following code converts a string that contains an IP address, in dotted-quad notation for IPv4 or in colon-hexadecimal notation for IPv6, into an instance of the IPAddress class. Then it uses the overloaded ToString method to display the address in standard notation.
using System; using System.Net; class ParseAddress { private static void Main(string[] args) { string IPaddress; if (args.Length == 0) { Console.WriteLine("Please enter an IP address."); Console.WriteLine("Usage: >cs_parse any IPv4 or IPv6 address."); Console.WriteLine("Example: >cs_parse 127.0.0.1"); Console.WriteLine("Example: >cs_parse 0:0:0:0:0:0:0:1"); return; } else IPaddress = args[0]; // Get the list of the IPv6 addresses associated with the requested host. parse(IPaddress); } // This method calls the IPAddress.Parse method to check the ipAddress // input string. If the ipAddress argument represents a syntatically correct IPv4 or // IPv6 address, the method displays the Parse output into quad-notation or // colon-hexadecimal notation, respectively. Otherwise, it displays an // error message. private static void parse(string ipAddress) { try { // Create an instance of IPAddress for the specified address string (in // dotted-quad, or colon-hexadecimal notation). IPAddress address = IPAddress.Parse(ipAddress); // Display the address in standard notation. Console.WriteLine("Parsing your input string: " + "\"" + ipAddress + "\"" + " produces this address (shown in its standard notation): "+ address.ToString()); } catch(ArgumentNullException e) { Console.WriteLine("ArgumentNullException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(FormatException e) { Console.WriteLine("FormatException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
To expand on Thomas Lee's correct remark:
IPAddress.Parse uses inet_addr under the hood to parse an IPv4 address. This makes the following rules valid:
1) Each part of the address can be either specified in decimal, octal or hexadecimal notation. A leading zero will indicate an octal number, a "0x" prefix will indicate an hexadecimal one. The following strings will all parse to the same address:
"16.16.16.16" // decimal
"020.020.020.020" // octal
"0x10.0x10.0x10.0x10" // hexadecimal
"16.020.0x10.16" // mixed base
2) Compatibility with Berkley software:
When a three-part address is specified, the last part is interpreted as a 16-bit quantity and placed in the right-most 2 bytes of the network address.
Example: "12.12.258" is valid and maps to 12.12.1.2
When a two-part address is specified, the last part is interpreted as a 24-bit quantity and placed in the right-most 3 bytes of the network address.
Example: "12.258" is valid and maps to 12.0.1.2
For further information, read the MSDN article on inet_addr.
- 5/25/2011
- Mario Cossi
- 5/25/2011
- Mario Cossi
- 2/23/2011
- Martin Buberl
<#
.SYNOPSIS
Demonstrates use of the GetHostEntry method of System.Net.DNS Class
.DESCRIPTION
This script is a an MSDN sample, using PowerShell. This script
uses the IP address of the host to find.
.NOTES
File Name : Get-HostEntry2.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2
.LINK
Sample posted to:
http://pshscripts.blogspot.com/
Original MSDN sample at:
http://msdn.microsoft.com/en-us/library/ms143997.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-HostEntry2.ps1 10.10.1.117
Host Name : cookham11.cookham.net
Alias :
Address : 10.10.1.117
.PARAM HostName
The name of the host to search for - the default is "127.0.0.1"
#>
param (
[string] $HostAddress = "127.0.0.1"
)
###
# Start of Script
###
# Convert $hostaddres to IPaddress class.
# Create one for next call
$HostIp = [System.Net.IPAddress]::Parse("127.0.0.1")
if (! ([system.Net.IPAddress]::TryParse($hostaddress, [ref] $HostIP))) {"Not valid IP address"; return}
# Get Host info
$hostentrydetails = [System.Net.Dns]::GetHostEntry($HostIP)
# Print details:
"Host Name : {0}" -f $hostentrydetails.HostName
foreach ($alias in $hostentrydetails.alises) {
"Alias : {0}" -f $alias
}
foreach ($addr in $hostentrydetails.addresslist) {
"Address : {0}" -f $Addr.ipaddresstostring
}
# End of script
- 10/5/2010
- Thomas Lee
IPAddress.Parse("100.10.1.0").ToString()
outputs: "100.10.1.0"
IPAddress.Parse("0100.010.1.0").ToString()
outputs: "64.8.1.0"
- 7/28/2010
- rtizan[0]
- 10/5/2010
- Thomas Lee