0

As part of migrating code base from PowerShell to Python, I need to covert the 'ConvertTo-Mask' function (available on PowerShell GET).

PowerShell function:

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))
    }
}

My Python Implementation:

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)

I am having difficulty with the '[Convert]::ToUInt32' part. If I give '$MaskLength = 14', the PowerShell function returns '4294705152'. But the python implementation returns '1119617024'.

Does anyone know where I am making a mistake?

3
  • data = uint32(("1"*14).ljust(32,"0")) ? Commented Mar 19, 2021 at 10:07
  • I used your code and I get an error: "Message=name 'uint32' is not defined" Commented Mar 19, 2021 at 10:54
  • Then perhaps How to convert signed to unsigned integer in python can be of help? Commented Mar 19, 2021 at 11:01

1 Answer 1

0

using int function, solved my problem. In my case, I had to convert from base 2.

Syntax: int(String,Base)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.