File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert 2 bytes to a a Int

 
Pedro Neves
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic