Michael Ansolis
Michael Ansolis

Reputation: 171

What's the proper way to initialize an unsigned integer to 0xFFFFFFFF in Powershell?

My CRC calculation algorithm requires that I initialize a 32-bit unsigned integer variable to 0xFFFFFFFF (all 1's in binary so that I can use the variable as a bitmask). If I just write [uint32]$r=0xFFFFFFFF, I get the following error message:

Cannot convert value "-1" to type "System.UInt32". Error: "Value was either too large or too small for a UInt32."

The syntax I'm currently using is [uint32]$r="0xFFFFFFFF", but it seems a bit over the top with the string to integer conversion (I'm coming from the C/C++ programming world). I'm pretty new to Powershell, so I was wondering if Powershell has a more efficient/straightforward way of initializing a variable like this.

Upvotes: 3

Views: 1568

Answers (2)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

PowerShell doesn't have any syntax for UInt32 literals, but you could cast an Int64 literal to [uint32] with the l type postfix:

[uint32]0xFFFFFFFFl

Upvotes: 1

vrdse
vrdse

Reputation: 3154

How about [UInt32]::MaxValue.

The value of this constant is 4,294,967,295; that is, hexadecimal 0xFFFFFFFF.

Upvotes: 6

Related Questions