bucrepus said:
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.