473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8635
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******** ******@tk2msftn gp13.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.ht m). 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.publi c.dotnet.langua ges.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******** ******@tk2msftn gp13.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 = callSomeFunctio n();
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*******@free net.de> wrote in message
news:eD******** ******@TK2MSFTN GP10.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 = callSomeFunctio n();
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.to int32(bitconver ter.getbytes(so meuint), 0)
Armin

Dec 10 '05 #8
Hi Armin, this works great, thanks for the answer.
"Armin Zingler" <az*******@free net.de> wrote in message
news:OY******** ******@TK2MSFTN GP09.phx.gbl...
"Chris Botha" <ch************ *@hotmail.com> schrieb
Here is the C# equivalent:

uint someUint = callSomeFunctio n();
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.to int32(bitconver ter.getbytes(so meuint), 0)
Armin

Dec 11 '05 #9
Chris,
In addition to the other comments.

The "problem" is that C# by default does not raise OverflowExcepti ons, 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 = callSomeFunctio n()
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 = callSomeFunctio n();
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******** ******@TK2MSFTN GP12.phx.gbl...
| Here is the C# equivalent:
|
| uint someUint = callSomeFunctio n();
| 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*******@free net.de> wrote in message
| news:eD******** ******@TK2MSFTN GP10.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
13034
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 these are ? I know that the
9
6219
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
5477
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 : %d\n",sizeof(unsigned char)); printf("short : %d\n",sizeof(short)); printf("unsigned short : %d\n",sizeof(unsigned short)); printf("int : %d\n",sizeof(int));
2
5441
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 and System::UInt32 is to be used. Now VB.NET does not even allow integer literals/variables to be assigned to a UInt32 variabe. Can somebody please explain the idea behind this and what type should I use in interfaces ?
1
1495
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 ' BackColor is UInt32 typ End Wit
2
1706
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 ' BackColor is UInt32 typ End Wit
20
4700
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 UInt32 Dim displayMask As UInt32 = Convert.ToUInt32(1) If (BitNum = Convert.ToUInt32(0)) Then
3
2082
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
15400
by: Yaniv | last post by:
Hi How can I convert Uint32 variable to System.drawing.color ?? Thanks in advanced Yaniv
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10148
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.