Powershell将IP地址转换为Integer

时间:2016-04-11 21:22:27

标签: python powershell integer ip

我想使用powershell将IP地址转换为整数。

当我使用python时,它的工作原理如下:

from netaddr import IPAddress
value1Numeric = int(IPAddress('10.36.5.0'))
print 'value1Numeric: '+str(value1Numeric)

输出值170132736,我期待。

然而,当我跟随these instructions在powershell中做同样的事情时,我看到了:

$IP = [IPAddress]"10.36.5.0".Trim()
$ip.Address

我从powershell的336906获得了一个值。

如何让PowerShell为我提供与python相同的价值?

1 个答案:

答案 0 :(得分:3)

您必须更改字节顺序。

[IPAddress]$ip = "255.255.255.255"
$bytes = $ip.GetAddressBytes()
if ([BitConverter]::IsLittleEndian) {
        [Array]::Reverse($bytes)
}
[BitConverter]::ToUInt32($bytes, 0)
相关问题