Conversion

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

    #1

    Conversion

    How can I strip the hi & low bytes from an integer. I have tried numerous
    code examples from the web and I can't get any to work. I have tried:
    // Get the low and high order words.
    int pintLow = pintValue and &Hffff;
    int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
    Have even tried VB.NET bit converter class, can't get it to give me the
    right values..
    Public Sub SplitInt2(ByVal Value As Integer, ByRef LoWord As Short,
    ByRef HiWord As Short)
    Dim buf(3) As Byte
    buf = GetBytes(Value)
    LoWord = ToInt16(buf, 0)
    HiWord = ToInt16(buf, 2)
    End Sub
    I tried to break the value 1000, it should return a 03 and 232 or 03E8 in
    hex.
    Any ideas how to break the value into high and low bytes, or am I doing this
    wrong..
    Thanks...
    BUC



  • Cor Ligthert [MVP]

    #2
    Re: Conversion

    Bucrepus,

    Are you trying to do something as you can with Cobol do.

    This is an integer.
    http://msdn.microsoft.com/library/de...classtopic.asp

    The way that it is stored is in my opinion not important for this. An int16
    is not two times an int16 it is just a structure that stores smaller values.

    Maybe you can just use a substract to get the most and less significant part
    of that.

    Cor

    "bucrepus" <meschreef in bericht
    news:OxHZj2fxGH A.2400@TK2MSFTN GP06.phx.gbl...
    How can I strip the hi & low bytes from an integer. I have tried numerous
    code examples from the web and I can't get any to work. I have tried:
    // Get the low and high order words.
    int pintLow = pintValue and &Hffff;
    int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
    Have even tried VB.NET bit converter class, can't get it to give me the
    right values..
    Public Sub SplitInt2(ByVal Value As Integer, ByRef LoWord As Short,
    ByRef HiWord As Short)
    Dim buf(3) As Byte
    buf = GetBytes(Value)
    LoWord = ToInt16(buf, 0)
    HiWord = ToInt16(buf, 2)
    End Sub
    I tried to break the value 1000, it should return a 03 and 232 or 03E8
    in hex.
    Any ideas how to break the value into high and low bytes, or am I doing
    this wrong..
    Thanks...
    BUC
    >
    >
    >

    Comment

    • bucrepus

      #3
      Re: Conversion

      I am just confused. I am trying to take the number 1000 (or any number
      between 0 and 32767) and get the high and low bytes from it. I guess it
      would be an Int16 in vb.net. 1000 in HEX in 03E8 which is a 3 and a 232. I
      have to put these 2 values into an array BYTE(1) = 3 and BYTE(2) = 232 and
      send via UDP packet.. I can do it in C easily, but can't figure the VB.NET
      way to break the INT16 up.
      Thanks BUC


      "bucrepus" <mewrote in message
      news:OxHZj2fxGH A.2400@TK2MSFTN GP06.phx.gbl...
      How can I strip the hi & low bytes from an integer. I have tried numerous
      code examples from the web and I can't get any to work. I have tried:
      // Get the low and high order words.
      int pintLow = pintValue and &Hffff;
      int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
      Have even tried VB.NET bit converter class, can't get it to give me the
      right values..
      Public Sub SplitInt2(ByVal Value As Integer, ByRef LoWord As Short,
      ByRef HiWord As Short)
      Dim buf(3) As Byte
      buf = GetBytes(Value)
      LoWord = ToInt16(buf, 0)
      HiWord = ToInt16(buf, 2)
      End Sub
      I tried to break the value 1000, it should return a 03 and 232 or 03E8
      in hex.
      Any ideas how to break the value into high and low bytes, or am I doing
      this wrong..
      Thanks...
      BUC
      >
      >
      >

      Comment

      • bucrepus

        #4
        Re: Conversion

        Ok never mind, I found a way, kind of goofy but works...
        Thanks for your time...
        BUC
        "bucrepus" <mewrote in message
        news:OxHZj2fxGH A.2400@TK2MSFTN GP06.phx.gbl...
        How can I strip the hi & low bytes from an integer. I have tried numerous
        code examples from the web and I can't get any to work. I have tried:
        // Get the low and high order words.
        int pintLow = pintValue and &Hffff;
        int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
        Have even tried VB.NET bit converter class, can't get it to give me the
        right values..
        Public Sub SplitInt2(ByVal Value As Integer, ByRef LoWord As Short,
        ByRef HiWord As Short)
        Dim buf(3) As Byte
        buf = GetBytes(Value)
        LoWord = ToInt16(buf, 0)
        HiWord = ToInt16(buf, 2)
        End Sub
        I tried to break the value 1000, it should return a 03 and 232 or 03E8
        in hex.
        Any ideas how to break the value into high and low bytes, or am I doing
        this wrong..
        Thanks...
        BUC
        >
        >
        >

        Comment

        • GhostInAK

          #5
          Re: Conversion

          Hello bucrepus" me,

          The results of:

          Dim tValue As Int16 = 1000
          Dim tBytes() As Byte

          tBytes = BitConverter.Ge tBytes(tValue)


          are:
          tBytes(0) = 232
          tBytes(1) = 3

          Enjoy,
          -Boo

          Ok never mind, I found a way, kind of goofy but works...
          Thanks for your time...
          BUC
          "bucrepus" <mewrote in message
          news:OxHZj2fxGH A.2400@TK2MSFTN GP06.phx.gbl...
          >How can I strip the hi & low bytes from an integer. I have tried
          >numerous
          >code examples from the web and I can't get any to work. I have tried:
          >// Get the low and high order words.
          >int pintLow = pintValue and &Hffff;
          >int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
          >Have even tried VB.NET bit converter class, can't get it to give me
          >the
          >right values..
          >Public Sub SplitInt2(ByVal Value As Integer, ByRef LoWord As Short,
          >ByRef HiWord As Short)
          >Dim buf(3) As Byte
          >buf = GetBytes(Value)
          >LoWord = ToInt16(buf, 0)
          >HiWord = ToInt16(buf, 2)
          >End Sub
          >I tried to break the value 1000, it should return a 03 and 232 or
          >03E8
          >in hex.
          >Any ideas how to break the value into high and low bytes, or am I
          >doing
          >this wrong..
          >Thanks...
          >BUC

          Comment

          • Branco Medeiros

            #6
            Re: Conversion


            bucrepus wrote:
            How can I strip the hi & low bytes from an integer. I have tried numerous
            code examples from the web and I can't get any to work. I have tried:
            // Get the low and high order words.
            int pintLow = pintValue and &Hffff;
            int pintHigh = (pintValue and &Hffff0000) >16 .. doesn't work
            <snip>

            and then, in another message, he added:
            I am just confused. I am trying to take the number 1000 (or any number
            between 0 and 32767) and get the high and low bytes from it. I guess it
            would be an Int16 in vb.net. 1000 in HEX in 03E8 which is a 3 and a 232. I
            have to put these 2 values into an array BYTE(1) = 3 and BYTE(2) = 232 and
            send via UDP packet.. I can do it in C easily, but can't figure the VB.NET
            way to break the INT16 up.
            Your first method would be perfect if not by the fact that it's getting
            the words (Int16, two bytes) from an Integer (Int32, four bytes), and
            not a byte (Int8, a single byte) from a word (Int16, two bytes):

            For instance, if you store 1000 in an Int32, you'll have (in hex):

            000003E8

            Using your original method, you'd have two Int16 vars, one containing
            0000 (the leftmost two bytes) and the other containing 03E8 (the
            rightmost two bytes).

            The first instruction, Value And &hFFFF, just masks the lower 16 bits.
            The second instruction, (Value And &hFFFF0000) >16, masks the upper
            16 bits (currently all zeroes) and moves them right by 16 bits,
            effectivelly putting then in the lower 16 bits. Supposing you were
            dealing with a bigger value, say, 305419896 (12345678 in hex), then
            you'd have:

            Lower = Value And &hFFFF '----&h5678
            Upper = (Value And &hFFFF0000) >16 '----&h1234

            To do what you want you must (appart from using the suggestion from
            Boo, of course), scale down your shifts and masks to the desired
            'precision', that is, Int16 and Int8:

            Dim Value As Word = 1000
            Dim Lower As Byte = Value And &hFF '----&h38
            Dim Upper As Byte = (Value And &hFF00) >8 '-----&h03

            HTH.

            Regards,

            Branco.

            Comment

            • Branco Medeiros

              #7
              Re: Conversion


              I wrote:
              Dim Value As Word = 1000
              Duh, this should actually be

              Dim Value As Short = 1000

              My bad.

              Regards,

              B.

              Comment

              Working...