Converting from Two’s complement using powershell

So first what is Two’s complement? Super simple simplification: It is how you can use the MSB (most significant bit) to define if the number is positive or negative. More complete and technical please read the Wikipedia article.

So why do I need this, Well in most cases you dont. But sometimes you get a int that should be unsigned but it was mangled, so you need a way to convert it to the true form. This cmdlet does that. Of course you could also just use the class System.BitConverter same way the cmdlet does.

Using the BitConverter to convert [int16] -1 to [uint16]:

For those that want to include a simple way of doing that in their profiles etc, I wrote a script.

What it does it takes input with with the name integer just the first argument and does its magic. I will show by int16 with the value -32000 but that should have been an unsigned int.

 

Leave a Reply