【发布时间】:2021-06-16 17:38:45
【问题描述】:
作为将代码库从 PowerShell 迁移到 Python 的一部分,我需要隐藏“ConvertTo-Mask”函数(在 PowerShell GET 上可用)。
PowerShell 函数:
Function ConvertTo-Mask {
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $True, Position = 0, ValueFromPipeline = $True)]
[Alias("Length")]
[ValidateRange(0, 32)]
$MaskLength
)
Process {
Return ConvertTo-DottedDecimalIP ([Convert]::ToUInt32($(("1" * $MaskLength).PadRight(32, "0")), 2))
}
}
我的 Python 实现:
import ctypes
def int32_to_uint32(i):
return ctypes.c_uint32(i).value
data = int(("1"*14).ljust(32,"0"))
print (data)
result = int32_to_uint32(data)
print (result)
我在使用“[Convert]::ToUInt32”部分时遇到了困难。如果我给出“$MaskLength = 14”,PowerShell 函数将返回“4294705152”。但是python实现返回'1119617024'。
有谁知道我哪里出错了?
【问题讨论】:
-
data = uint32(("1"*14).ljust(32,"0"))? -
我使用了您的代码,但出现错误:“Message=name 'uint32' is not defined”
标签: python powershell