UInt32 to Integer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Botha

    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.


  • Ray Cassick \(Home\)

    #2
    Re: UInt32 to Integer?

    VB does not support Unsigned integers. I assume you are getting an invalid
    cast exception?


    "Chris Botha" <chris_s_bothaA T@hotmail.com> wrote in message
    news:e65YkRP$FH A.2784@tk2msftn gp13.phx.gbl...[color=blue]
    > 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.
    >[/color]


    Comment

    • Armin Zingler

      #3
      Re: UInt32 to Integer?

      "Chris Botha" <chris_s_bothaA T@hotmail.com> schrieb[color=blue]
      > 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.[/color]

      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

      Comment

      • Matthew.Gertz@feedback.microsoft.com

        #4
        Re: UInt32 to Integer?

        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" <chris_s_bothaA T@hotmail.com> schrieb[color=blue]
        > 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.[/color]

        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

        Comment

        • Chris Botha

          #5
          Re: UInt32 to Integer?

          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" <chris_s_bothaA T@hotmail.com> wrote in message
          news:e65YkRP$FH A.2784@tk2msftn gp13.phx.gbl...[color=blue]
          > 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.
          >[/color]


          Comment

          • Armin Zingler

            #6
            Re: UInt32 to Integer?

            "Chris Botha" <chris_s_bothaA T@hotmail.com> schrieb[color=blue]
            > 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 :-([/color]


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

            Armin

            Comment

            • Chris Botha

              #7
              Re: UInt32 to Integer?

              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.nospam@free net.de> wrote in message
              news:eD8xkcc$FH A.2564@TK2MSFTN GP10.phx.gbl...[color=blue]
              > "Chris Botha" <chris_s_bothaA T@hotmail.com> schrieb[color=green]
              >> 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 :-([/color]
              >
              >
              > *What* do you want to do? Give us some context.
              >
              > Armin[/color]


              Comment

              • Armin Zingler

                #8
                Re: UInt32 to Integer?

                "Chris Botha" <chris_s_bothaA T@hotmail.com> schrieb[color=blue]
                > 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.[/color]


                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

                Comment

                • Chris Botha

                  #9
                  Re: UInt32 to Integer?

                  Hi Armin, this works great, thanks for the answer.


                  "Armin Zingler" <az.nospam@free net.de> wrote in message
                  news:OYlCssd$FH A.3568@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > "Chris Botha" <chris_s_bothaA T@hotmail.com> schrieb[color=green]
                  >> 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.[/color]
                  >
                  >
                  > 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
                  >[/color]


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: UInt32 to Integer?

                    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" <chris_s_bothaA T@hotmail.com> wrote in message
                    news:eCNAjQd$FH A.3372@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.nospam@free net.de> wrote in message
                    | news:eD8xkcc$FH A.2564@TK2MSFTN GP10.phx.gbl...
                    | > "Chris Botha" <chris_s_bothaA T@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
                    |
                    |


                    Comment

                    Working...