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.
Back in the VB6 days we had to write routines like this all the time:
At least in .NET you can create an extension method out of this to make it nicer through
I was just faced with this issue as well and didn't like the BitConverter approach as it seems like it's not very optimized. So, I considered that the storage of the data in memory is really just 4 bytes for both an int and uint.
The following seems to be the most efficient way to handle this and works in all .NET languages that can use the Marshal class...
Hope this helps someone.
Don't know VB, but I expect it is similar to C# as it is .NET code. In C# you can simply use type cast:
Done.
If you need to do this often, you can create extension methods like these:
This makes signed-unsigned conversions very simple:
Very simple:
For 32 bit
val32 ends up = -1294967296
For 16 bit
val16 ends up = -5536
Necromancing.
As a complement to Marc Gravell's answer, if you wonder how to do it in the head:
You can generally write it as:
Because of type-checking, code goes like this:
And then, if you want to do it in the head, you can do it like this: