Sign extend 16-bit value to Integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack Jackson

    #1

    Sign extend 16-bit value to Integer

    What is the best way to sign extend a 16-bit value (such as half of
    the lparam value in WM_NCxxx messages) into an Integer?

    Currently I test for the 8000 bit and if set OR in FFFF0000, but it
    seems like there should be some built-in way to do this.
  • James Hahn

    #2
    Re: Sign extend 16-bit value to Integer

    If it's a Short (Int16) it will widen correctly to Integer (Int32).

    "Jack Jackson" <jjackson@cinno vations.netwrot e in message
    news:st6ge457od qs45sbrs005u6b3 5ba4j7t8q@4ax.c om...
    What is the best way to sign extend a 16-bit value (such as half of
    the lparam value in WM_NCxxx messages) into an Integer?
    >
    Currently I test for the 8000 bit and if set OR in FFFF0000, but it
    seems like there should be some built-in way to do this.

    Comment

    • Family Tree Mike

      #3
      Re: Sign extend 16-bit value to Integer

      Try the following example for BitConverter to see if this works for you.

      Sub Main()
      Dim x32 As Int32 = &H432312AF
      Dim a16, b16 As Int16 ' if you want 2 byte ints
      Dim a32, b32 As Int32 ' if you want 4 byte ints
      Dim ba As Byte() = BitConverter.Ge tBytes(x32)

      a16 = BitConverter.To Int16(ba, 0)
      b16 = BitConverter.To Int16(ba, 2)

      a32 = a16 : b32 = b16

      Console.Out.Wri teLine(String.F ormat("{0:x} {1:x} {2:x}", x32, a32, b32))
      Console.In.Read Line()

      End Sub

      "Jack Jackson" <jjackson@cinno vations.netwrot e in message
      news:st6ge457od qs45sbrs005u6b3 5ba4j7t8q@4ax.c om...
      What is the best way to sign extend a 16-bit value (such as half of
      the lparam value in WM_NCxxx messages) into an Integer?
      >
      Currently I test for the 8000 bit and if set OR in FFFF0000, but it
      seems like there should be some built-in way to do this.

      Comment

      • Jack Jackson

        #4
        Re: Sign extend 16-bit value to Integer

        Thanks, that works fine.

        On Sun, 5 Oct 2008 16:53:34 -0400, "Family Tree Mike"
        <FamilyTreeMike @ThisOldHouse.c omwrote:
        >Try the following example for BitConverter to see if this works for you.
        >
        Sub Main()
        Dim x32 As Int32 = &H432312AF
        Dim a16, b16 As Int16 ' if you want 2 byte ints
        Dim a32, b32 As Int32 ' if you want 4 byte ints
        Dim ba As Byte() = BitConverter.Ge tBytes(x32)
        >
        a16 = BitConverter.To Int16(ba, 0)
        b16 = BitConverter.To Int16(ba, 2)
        >
        a32 = a16 : b32 = b16
        >
        Console.Out.Wri teLine(String.F ormat("{0:x} {1:x} {2:x}", x32, a32, b32))
        Console.In.Read Line()
        >
        End Sub
        >
        >"Jack Jackson" <jjackson@cinno vations.netwrot e in message
        >news:st6ge457o dqs45sbrs005u6b 35ba4j7t8q@4ax. com...
        >What is the best way to sign extend a 16-bit value (such as half of
        >the lparam value in WM_NCxxx messages) into an Integer?
        >>
        >Currently I test for the 8000 bit and if set OR in FFFF0000, but it
        >seems like there should be some built-in way to do this.

        Comment

        • Cor Ligthert

          #5
          Re: Sign extend 16-bit value to Integer

          Jack,

          Copied from a message in this newsgroup from Tom Spink

          \\\
          Dim strText = "The String Being Measured"
          Dim g As Graphics = Me.CreateGraphi cs
          Dim sfSize As SizeF = g.MeasureString (strText, Me.Font)

          MsgBox("Width: " & sfSize.Width & vbCrLf & "Height: " & sfSize.Height)
          ///

          Cor

          "Jack Jackson" <jjackson@cinno vations.netschr eef in bericht
          news:st6ge457od qs45sbrs005u6b3 5ba4j7t8q@4ax.c om...
          What is the best way to sign extend a 16-bit value (such as half of
          the lparam value in WM_NCxxx messages) into an Integer?
          >
          Currently I test for the 8000 bit and if set OR in FFFF0000, but it
          seems like there should be some built-in way to do this.

          Comment

          Working...