如何无符号整数转换成有符号整数,而不发生OverflowException [英] How to convert unsigned integer to signed integer without OverflowException

查看:712
本文介绍了如何无符号整数转换成有符号整数,而不发生OverflowException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够把一个高价值的无符号整数为有符号整数(即使用最高位的值)。在这种情况下,我不关心的值比符号整型的最大值高。我只是希望它转换成任何位值重新present作为一个有符号整数。换句话说,我希望它导致一个负数。

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.

然而,VB.NET,在 CTYPE 操作不工作的方式(或任何类似的其他转换功能 C短C CInteger )。当您尝试将一个无符号的值比期望的签署型的最高值时,它抛出一个发生OverflowException 而不是返回一个负数。例如:

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

这是值得一提,那就是,在 DirectCast 操作不能用于铸造的符号和无符号类型之间的价值,因为无论类型继承或实现其他的。例如:

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

就像我说的,这样的作品,但如果有做在VB.NET一个更简单,更清洁的方式,我很想知道它是什么。

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.

推荐答案

经常使用 BitConverter 的是怎么回事,如果你使用的是会有点不方便,很多 - 中特别是对于性能。如果是我,我会受到严峻的诱惑加水电费库在C#中,可以做直接转换(通过选中,虽然选中通常是默认在C#中的反正的),并参考该库为这个。另一种选择可能是滥用了联盟结构;下面应该翻译成很容易VB:

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;

<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

这篇关于如何无符号整数转换成有符号整数,而不发生OverflowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆