Home All Groups Group Topic Archive Search About
Author
22 Aug 2006 4:14 PM
bucrepus
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

Author
22 Aug 2006 3:50 PM
Cor Ligthert [MVP]
Bucrepus,

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

This is an integer.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemint32classtopic.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

Show quoteHide quote
"bucrepus" <me> schreef in bericht
news:OxHZj2fxGHA.2400@TK2MSFTNGP06.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
>
>
>
Author
22 Aug 2006 5:01 PM
bucrepus
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


Show quoteHide quote
"bucrepus" <me> wrote in message
news:OxHZj2fxGHA.2400@TK2MSFTNGP06.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
>
>
>
Author
22 Aug 2006 5:10 PM
bucrepus
Ok never mind, I found a way, kind of goofy but works...
Thanks for your time...
BUC
Show quoteHide quote
"bucrepus" <me> wrote in message
news:OxHZj2fxGHA.2400@TK2MSFTNGP06.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
>
>
>
Author
24 Aug 2006 4:40 AM
GhostInAK
Hello bucrepus" me,

The results of:

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

tBytes = BitConverter.GetBytes(tValue)


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

Enjoy,
-Boo


Show quoteHide quote
> Ok never mind, I found a way, kind of goofy but works...
> Thanks for your time...
> BUC
> "bucrepus" <me> wrote in message
> news:OxHZj2fxGHA.2400@TK2MSFTNGP06.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
Author
25 Aug 2006 3:32 AM
Branco Medeiros
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.
Author
25 Aug 2006 3:47 AM
Branco Medeiros
I wrote:

>   Dim Value As Word = 1000

Duh, this should actually be

    Dim Value As Short = 1000

My bad.

Regards,

B.