Conversion

B

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
 
B

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
 
B

bucrepus

Ok never mind, I found a way, kind of goofy but works...
Thanks for your time...
BUC
 
G

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
 
B

Branco Medeiros

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top