??problems typecasting in VB.NET??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • weg22@drexel.edu

    #1

    ??problems typecasting in VB.NET??

    Hi all,

    I'm having problems trying to type cast in VB.Net. I'd like to read
    in a 16 bit unsigned integer and convert it to a signed 16 bit
    integer. I'm using the following line of code:

    Dim wordVal As Short

    wordVal = CShort(Val(inpu t.Text))
    output.Text = wordVal

    Whenever input.Text 32767, I get a run-time error. Looking for some
    suggestions.

    Thanks in advance,
    -weg

  • Alexander Vasilevsky

    #2
    Re: ??problems typecasting in VB.NET??

    Try BitConverter class

    --
    http://www.alvas.net - Audio tools for C# and VB.Net developers


    <weg22@drexel.e du???????/???????? ? ???????? ?????????:
    news:1194271953 .671948.89130@v 23g2000prn.goog legroups.com...
    Hi all,
    >
    I'm having problems trying to type cast in VB.Net. I'd like to read
    in a 16 bit unsigned integer and convert it to a signed 16 bit
    integer. I'm using the following line of code:
    >
    Dim wordVal As Short
    >
    wordVal = CShort(Val(inpu t.Text))
    output.Text = wordVal
    >
    Whenever input.Text 32767, I get a run-time error. Looking for some
    suggestions.
    >
    Thanks in advance,
    -weg
    >

    Comment

    • Armin Zingler

      #3
      Re: ??problems typecasting in VB.NET??

      <weg22@drexel.e duschrieb
      Hi all,
      >
      I'm having problems trying to type cast in VB.Net. I'd like to read
      in a 16 bit unsigned integer and convert it to a signed 16 bit
      integer. I'm using the following line of code:
      >
      Dim wordVal As Short
      >
      wordVal = CShort(Val(inpu t.Text))
      output.Text = wordVal
      >
      Whenever input.Text 32767, I get a run-time error. Looking for
      some suggestions.
      I'm not sure what you are trying to do. If you want to input values 32767,
      why not use a different data type?


      Armin

      Comment

      • weg22@drexel.edu

        #4
        Re: ??problems typecasting in VB.NET??

        I'm not sure what you are trying to do. If you want to input values 32767,
        why not use a different data type?
        I'm not sure what you are trying to do. If you want to input values 32767,
        why not use a different data type?
        >
        Armin
        To convert from an unsigned int to a signed int, the code would read
        something like this:

        If unsignedInt 32767 Then
        signedInt = unsignedInt - 65535
        Else
        signedInt = unsignedInt
        End If

        I would like to do the above conversion by typecasting so I don't have
        to call a function like above every time I read in a value. I would
        rather do something like:

        signedInt = CShort(unsigned Int)

        but I get an overflow error.


        Comment

        • =?Utf-8?B?QU1lcmNlcg==?=

          #5
          Re: ??problems typecasting in VB.NET??

          To convert from an unsigned int to a signed int, the code would read
          something like this:
          >
          If unsignedInt 32767 Then
          signedInt = unsignedInt - 65535
          Else
          signedInt = unsignedInt
          End If
          >
          I would like to do the above conversion by typecasting so I don't have
          to call a function like above every time I read in a value. I would
          rather do something like:
          >
          signedInt = CShort(unsigned Int)
          >
          but I get an overflow error.

          Casting in VB usually implies a conversion. I assume from your original
          that you want to do something like sx=(SHORT)ux from the good/bad old days
          which does no conversion. The CShort() call is failing because it can't
          convert (not cast) 65000 into a Short.

          If you want economy and legibility of source code, call a function.

          If you really want to do an old C style cast in VB, then you probably have
          to make the equivalent of a union, eg


          <Runtime.Intero pServices.Struc tLayout(Runtime .InteropService s.LayoutKind.Ex plicit)Public Structure Union
          <Runtime.Intero pServices.Field Offset(0)Public xShort As Short
          <Runtime.Intero pServices.Field Offset(0)Public xUShort As UShort
          <Runtime.Intero pServices.Field Offset(0)Public xInteger As Integer
          <Runtime.Intero pServices.Field Offset(0)Public xIntPtr As IntPtr
          <Runtime.Intero pServices.Field Offset(0)Public xByte As Byte
          etc as with as much as you want.....
          End Structure

          Then you can have one of these things laying around, eg
          Dim z as Union
          and cast like this
          dim u as UShort
          dim s as Short = whatever
          z.xShort = s : u = z.xUShort ' no function calls, but two assignments

          I personally don't like this kind of thing in VB, and I think the function
          call is the better answer. But I think this gets you as close as possible in
          VB to the cast you are looking for.

          Comment

          • Armin Zingler

            #6
            Re: ??problems typecasting in VB.NET??

            <weg22@drexel.e duschrieb
            I'm not sure what you are trying to do. If you want to input
            values 32767, why not use a different data type?
            >
            I'm not sure what you are trying to do. If you want to input
            values 32767, why not use a different data type?

            Armin
            >
            To convert from an unsigned int to a signed int, the code would read
            something like this:
            >
            If unsignedInt 32767 Then
            signedInt = unsignedInt - 65535
            Else
            signedInt = unsignedInt
            End If
            >
            I would like to do the above conversion by typecasting so I don't
            have to call a function like above every time I read in a value. I
            would rather do something like:
            >
            signedInt = CShort(unsigned Int)
            >
            but I get an overflow error.
            /Casting/ is not possible because casting must be type safe (Short and
            UShort are not derived from each other). /Converting/ is possible, either
            they way you did above, or use the BitConverter class, as suggested by
            Alexander before (now that I know what you are trying I also
            suggest it).

            Dim s As Short
            Dim u As UShort = 40000
            s = BitConverter.To Int16(BitConver ter.GetBytes(u) , 0)

            But I guess your own function is faster.

            In any case, you should enable Option Strict On.

            Shared Function UShortToShort(B yVal u As UShort) As Short
            If u 32767 Then
            Return CShort(CInt(u) - 65536)
            Else
            Return CShort(u)
            End If
            End Function


            Armin

            Comment

            • Patrice

              #7
              Re: ??problems typecasting in VB.NET??

              This is expected as the max value for a short is 32767. So you'll never be
              able to "convert" a ushort value that is 40000 into a short value that would
              have the same absolute value (if you meant the internal representation
              you'll get a negative value and I'm not sure this is what you want as this
              could lead to wrong condition for whatever calculation you would do on this)

              For now, possible options would be :
              - use Ushort variables
              - use a wider datatype such as integer so that you can store a value such as
              40000 that you can't store as such in a Short
              - the last resort would be to use the same storage pattern but then the
              value will be negative so it really depends what you are trying to do and
              could be risky if you have some computation later in your code...

              <weg22@drexel.e dua écrit dans le message de news:
              1194271953.6719 48.89130@v23g20 00...legro ups.com...
              Hi all,
              >
              I'm having problems trying to type cast in VB.Net. I'd like to read
              in a 16 bit unsigned integer and convert it to a signed 16 bit
              integer. I'm using the following line of code:
              >
              Dim wordVal As Short
              >
              wordVal = CShort(Val(inpu t.Text))
              output.Text = wordVal
              >
              Whenever input.Text 32767, I get a run-time error. Looking for some
              suggestions.
              >
              Thanks in advance,
              -weg
              >

              Comment

              • Herfried K. Wagner [MVP]

                #8
                Re: ??problems typecasting in VB.NET??

                <weg22@drexel.e duschrieb:
                I'm having problems trying to type cast in VB.Net. I'd like to read
                in a 16 bit unsigned integer and convert it to a signed 16 bit
                integer. I'm using the following line of code:
                >
                Dim wordVal As Short
                >
                wordVal = CShort(Val(inpu t.Text))
                output.Text = wordVal
                >
                Whenever input.Text 32767, I get a run-time error. Looking for some
                suggestions.
                'Short' is a signed data type ('Int16'). You may want to use 'UShort'
                instead. I suggest to use 'UShort.TryPars e' to parse the user input instead
                of 'CShort(Val(... ))'.

                --
                M S Herfried K. Wagner
                M V P <URL:http://dotnet.mvps.org/>
                V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                Comment

                • =?Utf-8?B?Z3V5?=

                  #9
                  RE: ??problems typecasting in VB.NET??

                  You can not convert all unsigned int16s to signed int16s
                  An unsigned int16 has 16 bits of data, a signed int16 has a sign bit and 15
                  bits of data, so it will not fit.
                  You will have to convert it to an int32/integer

                  Guy

                  "weg22@drexel.e du" wrote:
                  Hi all,
                  >
                  I'm having problems trying to type cast in VB.Net. I'd like to read
                  in a 16 bit unsigned integer and convert it to a signed 16 bit
                  integer. I'm using the following line of code:
                  >
                  Dim wordVal As Short
                  >
                  wordVal = CShort(Val(inpu t.Text))
                  output.Text = wordVal
                  >
                  Whenever input.Text 32767, I get a run-time error. Looking for some
                  suggestions.
                  >
                  Thanks in advance,
                  -weg
                  >
                  >

                  Comment

                  • Phill W.

                    #10
                    Re: ??problems typecasting in VB.NET??

                    weg22@drexel.ed u wrote:
                    Dim wordVal As Short
                    wordVal = CShort(Val(inpu t.Text))
                    Whenever input.Text 32767, I get a run-time error.
                    Looking for some suggestions.
                    Well; what do you know?
                    The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
                    back again still works!

                    Dim unsigned as UInt16 = 40000
                    Dim signed as Int16 _
                    = CShort(Val("&H" & Hex(unsigned)))

                    HTH,
                    Phill W.

                    Comment

                    • =?Utf-8?B?Z3V5?=

                      #11
                      Re: ??problems typecasting in VB.NET??

                      Yes Phil, but 40000 then becomes -25536 :-)

                      Guy

                      "Phill W." wrote:
                      weg22@drexel.ed u wrote:
                      >
                      Dim wordVal As Short
                      wordVal = CShort(Val(inpu t.Text))
                      >
                      Whenever input.Text 32767, I get a run-time error.
                      Looking for some suggestions.
                      >
                      Well; what do you know?
                      The old, VB "Proper" trick of "side-stepping" out to Hexadecimal and
                      back again still works!
                      >
                      Dim unsigned as UInt16 = 40000
                      Dim signed as Int16 _
                      = CShort(Val("&H" & Hex(unsigned)))
                      >
                      HTH,
                      Phill W.
                      >

                      Comment

                      • Phill W.

                        #12
                        Re: ??problems typecasting in VB.NET??

                        guy wrote:
                        but 40000 then becomes -25536 :-)
                        Exactly what I would expect.

                        Read up about the Short Data Type and its limitations.

                        You can't fit a quart into a pint pot; if it holds the pint, it's doing
                        the best it can...

                        Regards,
                        Phill W.

                        Comment

                        Working...