UInt32 to Integer?

  • Thread starter Thread starter Chris Botha
  • Start date Start date
C

Chris Botha

Hi, I have an UInt32 and want to stick the value into an Integer and get an
Overflow exception, BUT using C# the same value can be casted into an int
and the value is as expected. The Hex value is FFFFFFDB, which should
be -37.

Thanks.
 
VB does not support Unsigned integers. I assume you are getting an invalid
cast exception?
 
Chris Botha said:
Hi, I have an UInt32 and want to stick the value into an Integer and
get an Overflow exception, BUT using C# the same value can be casted
into an int and the value is as expected. The Hex value is FFFFFFDB,
which should be -37.

In VB 2003, Uint32 is not supported. It is in VB 2005.

Where do you have the UInt32 value from? I ask because you are talking about
a hex string. If you already have a Uint32 value, you can convert it by
using System.Convert.ToInt32 - which will fail if the value exceeds the
Integer range.


Armin
 
That's right -- since FFFFFFDB is the value of an unsigned integer, it is assumed to be a positive integer of value 4294967259 (as opposed to just a collection of bytes), and that value won't fit into a signed integer. Hence, the overflow exception in this case, which happens in a narrowing conversion (see ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vbalr/html/058c3152-6c28-4268-af44-2209e774f0bd.htm). Integer casting in VB throws on casting where the value of the number changes.

(Note that the ToInt32 that Armin mentions below is used implicitly if you decide to assign the one to the other.)

--Matt Gertz--*
VB Compiler Dev Lead
-----Original Message-----
From: Armin Zingler
Posted At: Friday, December 09, 2005 12:20 PM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: UInt32 to Integer?
Subject: Re: UInt32 to Integer?


Chris Botha said:
Hi, I have an UInt32 and want to stick the value into an Integer and
get an Overflow exception, BUT using C# the same value can be casted
into an int and the value is as expected. The Hex value is FFFFFFDB,
which should be -37.

In VB 2003, Uint32 is not supported. It is in VB 2005.

Where do you have the UInt32 value from? I ask because you are talking about
a hex string. If you already have a Uint32 value, you can convert it by
using System.Convert.ToInt32 - which will fail if the value exceeds the
Integer range.


Armin
 
Hi guys, thanks for the answers, but I know what the problem is, and I am
looking for a workaround in VB. I can do it in C#, but this is a VB project
:-(

Thanks.
 
Chris Botha said:
Hi guys, thanks for the answers, but I know what the problem is, and
I am looking for a workaround in VB. I can do it in C#, but this is
a VB project :-(


*What* do you want to do? Give us some context.

Armin
 
Here is the C# equivalent:

uint someUint = callSomeFunction();
int someInt = (int)someUint;

After the function call the value of someUint is Hex FFFFFFDB (or then
4294967259 as noted by Matthew).
After casting it to an integer in C#, the second statement in my example,
the value of the integer is -37, which is what I want.

I am trying to achieve the same in VB.

Thanks again.
 
Chris Botha said:
Here is the C# equivalent:

uint someUint = callSomeFunction();
int someInt = (int)someUint;

After the function call the value of someUint is Hex FFFFFFDB (or
then 4294967259 as noted by Matthew).
After casting it to an integer in C#, the second statement in my
example, the value of the integer is -37, which is what I want.

I am trying to achieve the same in VB.


Casting in C# is different from casting in VB.Net. Casting in VB.Net means:
Changing the type of the reference, /and/ the type of the referenced object
must be the destination type or derived from it.

What you can do is:

someInt = bitconverter.toint32(bitconverter.getbytes(someuint), 0)


Armin
 
Chris,
In addition to the other comments.

The "problem" is that C# by default does not raise OverflowExceptions, while
by default VB does.

In VB 2005 you can use "Project Properties - Compile - Advanced Compile
Options - Remove integer overflow checks" to get the following assignment to
work:

Dim someUint As UInt32 = callSomeFunction()
Dim someInt As Integer = CType(someUint, Integer)

VB 2002 & 2003 don't support UInt32, so I'm not expecting the overflow check
option to help you much there!

Of course changing the "Remove integer overflow checks" may cause adverse
effects elsewhere in your code...

FWIW: "Project Properties - Build - Advanced Build Settings - Check for
arithmetic overflow/underflow" is the corresponding option in C#. For
example changing the "Check for arithmetic overflow/underflow" option will
cause your original code to fail, just as VB does!

uint someUint = callSomeFunction();
int someInt = (int)someUint;

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Here is the C# equivalent:
|
| uint someUint = callSomeFunction();
| int someInt = (int)someUint;
|
| After the function call the value of someUint is Hex FFFFFFDB (or then
| 4294967259 as noted by Matthew).
| After casting it to an integer in C#, the second statement in my example,
| the value of the integer is -37, which is what I want.
|
| I am trying to achieve the same in VB.
|
| Thanks again.
|
|
| | >> Hi guys, thanks for the answers, but I know what the problem is, and
| >> I am looking for a workaround in VB. I can do it in C#, but this is
| >> a VB project :-(
| >
| >
| > *What* do you want to do? Give us some context.
| >
| > Armin
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top