【问题标题】:Powershell 'ConvertTo-Mask' function to Python functionPowershell 'ConvertTo-Mask' 函数到 Python 函数
【发布时间】: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'。

有谁知道我哪里出错了?

【问题讨论】:

标签: python powershell


【解决方案1】:

使用 int 函数,解决了我的问题。就我而言,我必须从基数 2 转换。

语法:int(String,Base)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多