Converting to Signed Using Casting

by Mar 28, 2012

In a previous tip, you learned how to use the Bitconverter type to convert hexadecimals to signed integers. Here is another way that uses type conversion:

PS> 0xffff
65535
PS> 0xfffe
65534
PS> [int16]("0x{0:x4}" -f ([UInt32]0xffff))
-1
PS> [int16]("0x{0:x4}" -f ([UInt32]0xfffe))
-2

Twitter This Tip! ReTweet this Tip!