UInt32 到 Int32 [英] UInt32 to Int32
问题描述
如果我有一个返回 Int32 的 VB.Net 函数,但使用无符号 int (UInt32) 进行计算等.如何将值为3392918397"的变量MyUintVar32"转换为标准 Int32VB.Net?
If I have a VB.Net function that returns an Int32, but uses an unsigned int (UInt32) for calculations, etc. How can I convert a variable "MyUintVar32" with a value of say "3392918397 into a standard Int32 in VB.Net?
在 c# 中,如果我只是执行return (int)(MyUintVar32);",我会得到 -902048899,而不是错误.
In c# if I just do a "return (int)(MyUintVar32);", I get -902048899, not an error.
我尝试了几种不同的方法.c# 处理这些转换的方式与 VB.Net 有什么不同?
I've tried several different methods. What is the difference in the way c# handles these conversions versus VB.Net?
推荐答案
我意识到这是一个旧帖子,但问题尚未得到解答.我想认识的其他人:
I realize this is an old post, but the question has not been answered. Other people my want to know:
Dim myUInt32 As UInt32 = 3392918397
Dim myInt32 As Int32 = Convert.ToInt32(myUInt32.ToString("X"), 16)
反向操作:
myUInt32 = Convert.ToUInt32(myInt32.ToString("X"), 16)
此外,可以创建一个联合结构来轻松地在 Int32 和 UInt32 之间转换:
Also, one can create a union structure to easily convert between Int32 and UInt32:
Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Explicit)> _
Public Structure UnionInt32
<FieldOffset(0)> _
Public IntValue As Int32
<FieldOffset(0)> _
Public UIntValue As UInt32
End Structure
Dim MyUnionInt32 as UnionInt32
MyUnionInt32.UIntValue = 3392918397
Dim IntVal as Int32 = MyUnionInt32.UIntValue '= -902048899
反向操作:
MyUnionInt32.IntValue = -902048000
Dim UIntVal as UInt32 = MyUnionInt32.UIntValue '= 3392919296
干杯,TENware
这篇关于UInt32 到 Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!