Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Short [] to Byte [] Coversion Query  RSS feed

 
Fiona Richards
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello there,

I am using some example code to try and implement some Matlab capabilities in Java. One of the methods involves the conversion of a short[] to byte[].
Here is the code from the example


I am still new to this kind of thing, but this method seems a bit convoluted. Is there a simpler, easier to understand way to do the conversion?

Many thanks!
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is dreadfully convoluted. You don't need the unsignedShortArray and you don't don't need most of the rest of the code. You can extract the most significant byte of the short using the >>> operator to shift it 8 bits right and cast the result to a byte. You can extract the least significant byte just by casting the short to a byte. This means you just need to very simply deal with the bigEndian difference.
 
Fiona Richards
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
was what you had in mind something like this?



Im unsure what the '>>>' operater does, so im unsure whether 2 or 3 are needed. Also, there is a possibility that 0x00ff and 0xff00 are in the wrong order, as examples were quite confusing!
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The '>>>' operator does a right shift with no sign extension. That is, it shifts what it has to the right, and fills in the left with 0s. The regular right shift will fill in with the sign bit, that is, the bit in the far left position. However, both operators convert the operands to ints (4 byte integers) first, so in your example where you shift a short by 8 bits, there's effectively no difference between '>>' and '>>>' . Going step by step:

1. array[i] is a short (2 byte) integer
2. array[i] >>> 8 shifts the bits from the high order byte to the low order byte, and sets the high order byte to 0 for positives or 0xFF for negatives. (Technically, it's the third byte of the 4-byte int.)
3. (array[i] >>> 8) & (0xff00) 0s out all but the high order byte of the shifted short (i.e., the third byte of the 4 byte int), and thus is either 0 or 0xFF00 for any possible input.

That's probably not what you want. I suggest just getting your converter working with big-endian shorts first, since that's Java's natural ordering. Then try some sample inputs and make sure the conversion is correct. Once you get that working, you can tackle little-endian shorts.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fiona Richards wrote:was what you had in mind something like this?



Im unsure what the '>>>' operater does, so im unsure whether 2 or 3 are needed. Also, there is a possibility that 0x00ff and 0xff00 are in the wrong order, as examples were quite confusing!


Since each short is converted to two bytes you need to extract the two bytes from each short. So for each short you simply construct

and fill the 'out' array.

Note - Your Java book will tell you about >>> . I normally use >>> but in this case you can use both >>> and >> and get the same result.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!