How to convert unsigned integer to signed integer without OverflowException
Back in the VB6 days we had to write routines like this all the time:
Private Function ToShort(ByVal us As UShort) As Short
If (us And &H8000) = 0 Then
Return CType(us, Short)
Else
Return CType(CType(us, Integer) - UShort.MaxValue - 1, Short)
End If
End Function
At least in .NET you can create an extension method out of this to make it nicer through
I think the easiest way is as follows:
Public Function PutSign(ByVal number As UShort) As Short
If number > 32768 Then 'negative number
Return (65536 - number) * -1
Else
Return number
End If
End Function
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