Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Short [] to Byte [] Coversion Query

Fiona Richards
Greenhorn
Posts: 4
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
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
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: 3015
12
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
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.

 It is sorta covered in the JavaRanch Style Guide.