How to convert unsigned integer to signed integer without OverflowException

Steven Doggart picture Steven Doggart · Feb 5, 2013 · Viewed 12.3k times · Source

I would like to be able to convert a high-valued unsigned-integer (a value that uses the highest-order bit) to a signed-integer. In this case, I don't care that the value is higher than the maximum value of the signed integer type. I just want it to convert to whatever the bit-values represent as a signed-integer. In other words, I would expect it to result in a negative number.

However, with VB.NET, the CType operation doesn't work that way (or any of the other conversion functions like CShort andCInteger). When you try to convert an unsigned value that is higher than the desired signed-type's maximum value, it throws an OverflowException rather than returning a negative number. For instance:

Dim x As UShort = UShort.MaxValue
Dim y As Short = CShort(x)  ' Throws OverflowException

It's worth mentioning, too, that the DirectCast operation cannot be used to cast the value between the signed and unsigned types, since neither type inherits or implements the other. For instance:

Dim x As UShort = UShort.MaxValue
Dim y As Short = DirectCast(x, Short)  ' Won't compile: "Value of type 'UShort' cannot be converted to 'Short'

I have figured out one way to do what I want, but it seems unnecessarily ugly. Here's how I got it to work:

Dim x As UShort = UShort.MaxValue
Dim y As Short = BitConverter.ToInt16(BitConverter.GetBytes(x), 0)  ' y gets set to -1

Like I said, that works, but if there's an easier, cleaner way of doing it in VB.NET, I'd love to know what it is.

Answer

Marc Gravell picture Marc Gravell · Feb 5, 2013

Constant use of BitConverter is going to be a bit inconvenient if you are using that a lot - in particular for performance. If that was me, I would be sorely tempted to add a utilities library in C# that can do direct conversions (via unchecked, although unchecked is normally the default in C# anyway), and reference that library for this. Another option might be to abuse a "union" struct; the following should translate to VB fairly easily:

[StructLayout(LayoutKind.Explicit)]
struct EvilUnion
{
    [FieldOffset(0)] public int Int32;
    [FieldOffset(0)] public uint UInt32;
}
...
var evil = new EvilUnion();
evil.Int32 = -123;
var converted = evil.UInt32;

i.e.

<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Explicit)>
Structure EvilUnion
    <System.Runtime.InteropServices.FieldOffset(0)>
    Public Int32 As Integer
    <System.Runtime.InteropServices.FieldOffset(0)>
    Public UInt32 As UInteger
End Structure
...
Dim evil As New EvilUnion
evil.Int32 = -123
Dim converted = evil.UInt32