• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert 2 bytes to a a Int

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 don't get it. A whale wearing overalls? How does that even work? It's like a tiny ad wearing overalls.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic