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?

Manjunath Rao
  • 1,397
  • 4
  • 26
  • 42

1 Answers1

0

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

Syntax: int(String,Base)

Manjunath Rao
  • 1,397
  • 4
  • 26
  • 42