Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Convert 2 bytes to a a Int

Pedro Neves
Ranch Hand
Posts: 64
Helo, i'm having a problem and i would apreciate some help of you

008 000 111 000 002 000 (bytes represented as an integer )

how can i put the tow bytes on a single integer? i mean, for example, 008 and 000 are from the same word, how can i join them on, for example the LOWORD of an integer?

code:

DataInputStream in = new DataInputStream(skt.getInputStream());

try{ num = in.read(dyn_data); } //catching the bytes
catch(IOException i){ System.out.print("Error\n"); } //

Arr = new int[num];

for(int i=0 ; i < num ; i++)
Arr[i] =((int) dyn_data[i] & 0xff); // conversion to int -- i know it would not make sense anymore

Henry Wong
author
Marshal
Posts: 21117
78
Pedro Neves wrote:Helo, i'm having a problem and i would apreciate some help of you

008 000 111 000 002 000 (bytes represented as an integer )

how can i put the tow bytes on a single integer? i mean, for example, 008 and 000 are from the same word, how can i join them on, for example the LOWORD of an integer?

code:

Assuming you have two bytes (that is held in ints -- shown above), you can use the shift operators to move the byte value into position and use the OR operator to merge the two "bytes".

For example, if you have the two low order bytes, with "a" being the lowest, and "b" being next, then the value is...

Henry

Winston Gutkowski
Bartender
Posts: 10417
63
Pedro Neves wrote:how can i put the tow bytes on a single integer? i mean, for example, 008 and 000 are from the same word, how can i join them on, for example the LOWORD of an integer?

It depends a bit on exactly what you want to achieve. Henry's suggestion will sign-extend b before shifting it (Java bytes are signed, don't forget).
This usually isn't an issue, but your masking method allied with his, viz:
int value = ((b & 0xff) << 8) & a;
will ensure that the leftmost 16 bits are 0.

HIH

Winston

Henry Wong
author
Marshal
Posts: 21117
78
Winston Gutkowski wrote:
It depends a bit on exactly what you want to achieve. Henry's suggestion will sign-extend b before shifting it (Java bytes are signed, don't forget).
This usually isn't an issue, but your masking method allied with his, viz:
int value = ((b & 0xff) << 8) & a;
will ensure that the leftmost 16 bits are 0.

Actually, you don't have to worry about that -- both bytes are already unsigned byte values, which are held in int variables. See original code (which loads the bytes into an int array)....

Henry

Pedro Neves
Ranch Hand
Posts: 64
Henry Wong wrote:
Winston Gutkowski wrote:
It depends a bit on exactly what you want to achieve. Henry's suggestion will sign-extend b before shifting it (Java bytes are signed, don't forget).
This usually isn't an issue, but your masking method allied with his, viz:
int value = ((b & 0xff) << 8) & a;
will ensure that the leftmost 16 bits are 0.

Actually, you don't have to worry about that -- both bytes are already unsigned byte values, which are held in int variables. See original code (which loads the bytes into an int array)....

Henry

Many thanks Henry Wong's solution really worked out ;)

kind regards to all