Converting to Signed

by Mar 27, 2012

If you convert a hex number to a decimal, the result may not be what you want:

PS> 0xFFFF
65535

PowerShell converts it to an unsigned number (unless its value is too large for an unsigned integer). If you need the signed number, you would have to use the Bitconverter type and first make the hex number a byte array, then convert this back to a signed integer like this:

PS> [BitConverter]::ToInt16([BitConverter]::GetBytes(0xFFFF), 0)
-1

Twitter This Tip! ReTweet this Tip!