Converting a string with an IPv4 address to an integer with PowerShell
Contents
Function description
The function Convert-StringIPToIntIP converts a string with an IP address to its integer representation. For example, the function will convert value “192.168.10.2” of type String to 1100 0000 1010 1000 0000 1010 0000 0010 of type Int32.
Function parameter:
- IPAddressString — string with an IP address, for example: "192.168.10.2". Range of acceptable addresses: 0.0.0.0–255.255.255.255.
Return value:
- Int32 with IP address if the string passed to the function contains a valid IP address.
Usage example:
The function itself
...is very straight forward:
The input is validated with the ValidatePattern attribute to ensure that the string passed to the function contains an IP address: four numbers in range 0–255 separated by a dot symbol.
The foreach comdlet initializes variables that will be used at each iteration:
- $IPAddress — Int32 that accumulates the result;
- $IPAddressByte — Int32 that contains current byte of the IP address;
Then foreach processes each byte of the IP address:
- Int32.TryParse method tries to convert each string passed to it ($_) to Int32 and save the result to $IPAddressByte. Also it returns $True or $False to indicate whether the conversion was sucessful. We suppress this output ($True or $False) by passing it to Out-Null;
- the resulting variable $IPAddress is then shifted left by 8 bits and $IPAddressByte is written to the 8 less significant bits of the $IPAddress.
After all the iterations have been completed, $IPAddress contains all the bytes from the input string.
Validating function input
To make sure that the string passed to the function contains a valid IP address, the ValidatePattern attribute is used, that allows to specify a regular expression that parameter values should match. If you are not familiar with regular expressions, here is an illustration that will help you to sort things out:
Step-by-step walkthrough
Here is the example of how the conversion is done for the "192.168.10.2" address:
-
Create Int32 variables that will contain the result and the current byte of the IP address:
$IPAddress = 0; $IPAddressByte = 0
-
Split the string "192.168.10.2" into array of four strings: { "192", "168", "10", "2" }.
$IPAddressString.Split(".")
-
For each string in the array:
-
Convert each string from the array to Int32:
[int]::TryParse($_, [ref] $IPAddressByte) | Out-Null
-
Shift the result ($IPAddress) left by 8 bits and add $IPAddressByte to it:
$IPAddress = $IPAddress -shl 8 -bor $IPAddressByte
-
Convert IP address byte from String to Int32:
[String] 192 = [Int32] 0000 0000 0000 0000 0000 0000 1100 0000
-
Shift the resulting Int32 variable left by 8 bits:
0000 0000 0000 0000 0000 0000 0000 0000 << 8 --------------------------------------------- 0000 0000 0000 0000 0000 0000 0000 0000
-
Add IP address byte to the result:
0000 0000 0000 0000 0000 0000 0000 0000 OR 0000 0000 0000 0000 0000 0000 1100 0000 --------------------------------------------- 0000 0000 0000 0000 0000 0000 1100 0000
-
Convert IP address byte from String to Int32:
[String] 168 = [Int32] 0000 0000 0000 0000 0000 0000 1010 1000
-
Shift the resulting Int32 variable left by 8 bits:
0000 0000 0000 0000 0000 0000 1100 0000 << 8 --------------------------------------------- 0000 0000 0000 0000 1100 0000 0000 0000
-
Add IP address byte to the result:
0000 0000 0000 0000 1100 0000 0000 0000 OR 0000 0000 0000 0000 0000 0000 1010 1000 --------------------------------------------- 0000 0000 0000 0000 1100 0000 1010 1000
-
Convert IP address byte from String to Int32:
[String] 10 = [Int32] 0000 0000 0000 0000 0000 0000 0000 1010
-
Shift the resulting Int32 variable left by 8 bits:
0000 0000 0000 0000 1100 0000 1010 1000 << 8 --------------------------------------------- 0000 0000 1100 0000 1010 1000 0000 0000
-
Add IP address byte to the result:
0000 0000 1100 0000 1010 1000 0000 0000 OR 0000 0000 0000 0000 0000 0000 0000 1010 --------------------------------------------- 0000 0000 1100 0000 1010 1000 0000 1010
-
Convert IP address byte from String to Int32:
[String] 2 = [Int32] 0000 0000 0000 0000 0000 0000 0000 0010
-
Shift the resulting Int32 variable left by 8 bits:
0000 0000 1100 0000 1010 1000 0000 1010 << 8 --------------------------------------------- 1100 0000 1010 1000 0000 1010 0000 0000
-
Add IP address byte to the result:
1100 0000 1010 1000 0000 1010 0000 0000 OR 0000 0000 0000 0000 0000 0000 0000 0010 --------------------------------------------- 1100 0000 1010 1000 0000 1010 0000 0010
-
Convert each string from the array to Int32:
- Return the result — 1100 0000 1010 1000 0000 1010 0000 0010, stored in $IPAddress.
Using the function: checking whether the IPv4 address belongs to the network
Here is an example of how we can use the created function. Let’s say we want to check whether the IP address belongs to the network. For instance, does the address 172.16.27.2 fall into the range of addresses of the network 172.16.24.0/22? In order to do such check, let’s create a couple more functions:
- Test-IfIPAddressBelongsToNetwork — performs the mentioned check;
- Create-MaskInt — a helper function that creates an integer with a network mask of the specified length.
Now we can use this function to do some simple checks:
Or something more complex:
Select IP addresses that belong to the specified networks:
Select IP addresses that do not belong to the specified networks: