Is there a way to declare a 32-bit unsigned integer in PowerShell?
I'm trying to add an unsigned (begin with 0xf
) i.e. 0xff000000 + 0xAA
, but it turned out to be a negative number whereas I want it to be 0xff0000AA
.
The code from the currently accepted answer causes the following error on my system:
This is because PowerShell always tries to convert hex values to int first so even if you cast to uint64
, the environment will complain if the number has a negative int value. You can see this from the following example:
PS C:\> [uint64] (-1)
Cannot convert value "-1" to type "System.UInt64". Error: "Value was either too large or too small for a UInt64."
At line:1 char:26
+ Invoke-Expression $ENUS; [uint64] (-1)
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastIConvertible
You need to cast the string representation of the number to uint64
to avoid this:
[uint64]"0xff000000" + [uint64]"0xAA"