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.
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 (viaunchecked
, althoughunchecked
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:i.e.
I think the easiest way is as follows:
I found this: ??problems typecasting in VB.NET??
About halfway down the page is this:
It seems to work pretty slick!
Normally, this would be done with streams in higher level languages, but .Net framework exposes a way to do so without intermediate stream objects using Marshal.
To my surprise, Using Marshal seems to be more efficient than using Math, based on the stats I got
from test:
Stranger still, Marshal approach seems negligibly as effective as C# style cast. On my first run, marshal approach was slower, but on second run, marshal approach was faster. This is the result of the second run
using this code
Now to bring in the king;
well, the results are pretty interesting