Converting unsigned to signed integers (using Powershell or Excel)

Let’s assume you got unsigned 32 bit integers, that actually represent 32 bit signed integers (using 2’s complement). How to get back the original (negative) values?

The formula is pretty easy:

signed = (raw+2^31) % 2^32 - 2^31

In Excel:

=MOD(A1+2^31;2^32)-2^31

In Powershell (2.0):

Function Convert-UInt-To-SInt($input)
{
return ( $inputAsInt + [Math]::Pow(2, 31) ) % [Math]::Pow(2, 32) - [Math]::Pow(2, 31)
}

In Groovy:

(raw + (1L << 31)) % ( 1L << 32) - ( 1L << 31 )

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s