• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Short [] to Byte [] Coversion Query

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic