aspose file tools*
The moose likes Java in General and the fly likes Convert 2 bytes to a a Int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert 2 bytes to a a Int" Watch "Convert 2 bytes to a a Int" New topic
Author

Convert 2 bytes to a a Int

Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Helo, i'm having a problem and i would apreciate some help of you
From the follow output:

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






Thanks in advance
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Pedro Neves wrote:Helo, i'm having a problem and i would apreciate some help of you
From the follow output:

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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

Joined: Mar 07, 2012
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert 2 bytes to a a Int