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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…