The IPv4 address can actually be replaced with a long plastic digit. When using the numeric type to represent the IP address, it can be very convenient to match and compare the address range. In the .NET development environment, an IPAddress class has an Address property that is a decimal number, while an IPAddressToString property is a dotted decimal string form that we are familiar with.
The following two filters process the conversion of "dotted decimal" string to number and the conversion of "dotted decimal" respectively.
Copy the codeThe code is as follows:
//Conversion of "dotted decimal" string to number
filter Convert-IP2Decimal
{
([IPAddress][String]([IPAddress]$_)).Address
}
//Conversion of numbers to "dotted decimal"
filter Convert-Decimal2IP
{
([]$_).IPAddressToString
}
When using it, import these two filters into the current environment and then execute using the pipeline.
Copy the codeThe code is as follows:
PS C:\Documents and Settings\Administrator> filter Convert-IP2Decimal
>> {
>> ([IPAddress][String]([IPAddress]$_)).Address
>> }
>>
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> filter Convert-Decimal2IP
>> {
>> ([]$_).IPAddressToString
>> }
>>
>> {
>> ([IPAddress][String]([IPAddress]$_)).Address
>> }
>>
PS C:\Documents and Settings\Administrator>
PS C:\Documents and Settings\Administrator> filter Convert-Decimal2IP
>> {
>> ([]$_).IPAddressToString
>> }
>>
Test the conversion effect:
Copy the codeThe code is as follows:
PS C:\Documents and Settings\Administrator> "192.168.0.1" | Convert-IP2Decimal
16820416
PS C:\Documents and Settings\Administrator> 16820416 | Convert-Decimal2IP
192.168.0.1
PS C:\Documents and Settings\Administrator>
16820416
PS C:\Documents and Settings\Administrator> 16820416 | Convert-Decimal2IP
192.168.0.1
PS C:\Documents and Settings\Administrator>