SIGN UP MEMBER LOGIN:    
ARTICLE

How to convert unsigned integer arrays to signed arrays and vice versa

Posted by Vulpes Articles | Current Affairs March 18, 2011
Here's a simple technique for converting between signed and unsigned integer arrays.
Reader Level:

Introduction

Here's a simple technique for converting between signed and unsigned integer arrays.

The problem

Suppose you have this unsigned array and you'd like to convert it to a signed array.

uint[] uia = { 1, 2, 3, 4 };

Let's try a simple cast first:

int[] ia = (int[])uia;  // doesn't compile!

This doesn't work because the C# compiler is very fussy. It knows that casting uints to ints might cause a problem if the uints are larger than 2,147,483,647 (the maximum allowed for an int) and so disallows it.

So, you'd probably end up doing something like this:

int[] ia = new int[uia.Length];
for (int i = 0; i < ia.Length; i++) ia[i] = (int)uia[i];


Which works but is a bit long winded. So is there an easier way to do?

An easy solution

The answer is 'yes' - just cast it to object before you cast to the new array type!

int[] ia = (int[])(object)uia; // works fine at both compile time and runtime

Why does this work - does it involve boxing?

It works because, by casting to object first, we're postponing the conversion decision until run time.

The CLR is not as fussy as the C# compiler and allows the conversion. If any uint is bigger than Int32.MaxValue (not the case here) it's simply 'wrapped around' to the corresponding negative int value.

As arrays are reference types anyway, there's no boxing here.

Where else does this technique work?

It also works when converting int[] to uint[] and when converting (in either direction) between the other integral types:

byte[] and sbyte[]
ushort[] and short[]
ulong[] and long[]

Of course, you need to be careful when array elements are outside the range of the conversion type (particularly with bytes) unless you're happy for the values to be wrapped around.
 

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor