6

setInstance(ByVal instance As UInteger)在我的VB.NET代码中使用库调用。我需要传递的参数是一个Integer. 我需要做些什么来将整数参数转换为无符号整数吗?该数字保证为正且小于 10。

4

3 回答 3

7

像这样...

Dim MyInt As Int32 = 10
Dim MyUInt As UInt32 = CUInt(MyInt)
setInstance(MyUInt)
于 2010-07-16T16:44:37.300 回答
3

CUInt 或 CType(x, UInt) 允许转换正整数

当 x 为负时,它会引发异常。

要将 Int 用作 Uint,您可以使用一些技巧:

  dim bb() = System.BitConverter.GetBytes(myInt)
  dim MyUint = System.BitConverter.ToUInt32(bb, 0)

System.Buffer.BlockCopy 也用于数组。

如果您将编译器配置为禁用检查整数溢出(C# 的默认设置)。然后,您可以将CUInt与负值一起使用而无需检查 - 不例外。

于 2010-07-16T17:45:44.223 回答
1

您可以调用CUint将变量转换为UInteger.

于 2010-07-16T16:33:31.117 回答