473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

UInt32 to Integer?

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.
Dec 9 '05 #1
9 8570
VB does not support Unsigned integers. I assume you are getting an invalid
cast exception?
"Chris Botha" <ch*************@hotmail.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
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.

Dec 9 '05 #2
"Chris Botha" <ch*************@hotmail.com> schrieb
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

Dec 9 '05 #3
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" <ch*************@hotmail.com> schrieb
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
Dec 9 '05 #4
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" <ch*************@hotmail.com> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
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.

Dec 10 '05 #5
"Chris Botha" <ch*************@hotmail.com> schrieb
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
Dec 10 '05 #6
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.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:eD**************@TK2MSFTNGP10.phx.gbl...
"Chris Botha" <ch*************@hotmail.com> schrieb
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

Dec 10 '05 #7
"Chris Botha" <ch*************@hotmail.com> schrieb
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(someuin t), 0)
Armin

Dec 10 '05 #8
Hi Armin, this works great, thanks for the answer.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
"Chris Botha" <ch*************@hotmail.com> schrieb
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(someuin t), 0)
Armin

Dec 11 '05 #9
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
"Chris Botha" <ch*************@hotmail.com> wrote in message
news:eC**************@TK2MSFTNGP12.phx.gbl...
| 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.
|
|
| "Armin Zingler" <az*******@freenet.de> wrote in message
| news:eD**************@TK2MSFTNGP10.phx.gbl...
| > "Chris Botha" <ch*************@hotmail.com> schrieb
| >> 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
|
|
Dec 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David Fisher | last post by:
The most common sizes of integer types seem to be: 8 bits - signed char 16 bits - short 16 or 32 bits - int 32 or 64 bits - long Question #1: Does anyone know how common sizes other than...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
35
by: Sunil | last post by:
Hi all, I am using gcc compiler in linux.I compiled a small program int main() { printf("char : %d\n",sizeof(char)); printf("unsigned char : ...
2
by: Shankar | last post by:
Hi, I am writing a Managed C++ class library where interfaces take a unsigned int parameter. Looking at the CTS documentation, it is mentioned that there is no built- in type in VB.NET for this...
1
by: steellock | last post by:
The below code always get compile error, "Can not change Interger to UInt32". Then How to set the BackColor property With TextObjec .Name = "txtProductCode .SetText("Apple" .BackColor = 8 '...
2
by: steellock | last post by:
The below code always get compile error, "Can not change Integer to UInt32". Then how can I set BackColor property With txtob .Name = "txtProductCode .SetText("Apple" .BackColor = 8 '...
20
by: barbara | last post by:
Hi, all I have to read a binary data which is four bytes and I need to use the following function to deal with data: Private Function BIT(ByVal Val As UInt32, ByVal BitNum As UInt32) As...
3
by: Ron | last post by:
Dim x As UInt32 x = 1000 Value of type 'Integer' cannot be converted to 'System.Uint32'. Why is this illegal?
11
by: Yaniv | last post by:
Hi How can I convert Uint32 variable to System.drawing.color ?? Thanks in advanced Yaniv
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.