Converting shortto byteand vice versa.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Converting shortto byteand vice versa.

  1. #1
    Stefan Buynov Guest

    Converting short[] to byte[] and vice versa.

    Hi,

    I have an Encoder/Decoder class, which uses some compression algorithm.
    The encoder gets a byte[] and returns a short[]. I need to write the
    resulting array in an OutputStream, read it at the client, convert it to
    short[] again (the decoder gets short[] and returns byte[]). I noticed
    that Short class has a byteValue() method, but this method might lead to
    an information loss if the short value is out of the byte type range -
    short is 16-bits, and byte is just 8.

    How can I do that? How to convert a short[] to a byte[] and byte[] to
    short[] without loosing information?
    TIA.

    Regards,
    Stefan


  2. #2
    Colin McGuigan Guest

    Re: Converting short[] to byte[] and vice versa.

    Stefan Buynov wrote:
    > Hi,
    >
    > I have an Encoder/Decoder class, which uses some compression
    > algorithm. The encoder gets a byte[] and returns a short[]. I need to
    > write the resulting array in an OutputStream, read it at the client,
    > convert it to short[] again (the decoder gets short[] and returns
    > byte[]). I noticed that Short class has a byteValue() method, but
    > this method might lead to an information loss if the short value is
    > out of the byte type range - short is 16-bits, and byte is just 8.
    >
    > How can I do that? How to convert a short[] to a byte[] and byte[] to
    > short[] without loosing information?


    Hrmm. Not sure if there's a better way in the Java API (it's so easy to
    miss something), but there's a way to do it manually. Create your byte
    array to be twice the size of your short array. For every short, enter
    two items in the byte array: the first is the lower byte of the short,
    and the second is the higher. When you're recoding to a short array,
    reverse it.

    (Warning: vapor code)

    // I assume you know how bit manipulation works. Otherwise, the
    following
    // may or may not make sense.
    public byte[] encodeShort(short[] s) {
    byte[] b = new byte[s.length * 2];

    for (int ii = 0; ii < s.length; ii++) {
    // This gets the lower byte of the short
    // I don't know if the '& 0x00001111' is really
    // necessary -- the conversion to byte might do
    // that automatically -- but better safe than sorry.
    b[ii * 2] = (byte)(s[ii] & 0x00001111);
    // This gets the higher byte of the short
    b[ii * 2 + 1] = (byte)((s[ii] >> 4) & 0x00001111);
    }
    return b;
    }

    public short[] decodeShort(byte[] b) {
    short[] s = new short[b.length / 2];

    for (int ii = 0; ii < s.length; ii++) {
    // This just reverses the above. It could be broken out onto
    // two lines for readability, if needed.
    s[ii] = (short)(b[ii * 2] | ((short)b[ii * 2 + 1] << 4));
    }
    return s;
    }

    As a final word of warning, I always get confused about whether the low
    byte or high byte should be sent first across the network. If I'm wrong
    on this (low byte first), I'm sure someone will step in and correct me
    shortly. It doesn't make a difference as to whether the code works, but
    it's good to follow standards.

    --
    Colin McGuigan



  3. #3
    Stefan Buynov Guest

    Re: Converting short[] to byte[] and vice versa.

    Thanks, seems to work )

    Colin McGuigan wrote:
    > Stefan Buynov wrote:
    >
    >> Hi,
    >>
    >>I have an Encoder/Decoder class, which uses some compression
    >>algorithm. The encoder gets a byte[] and returns a short[]. I need to
    >>write the resulting array in an OutputStream, read it at the client,
    >>convert it to short[] again (the decoder gets short[] and returns
    >>byte[]). I noticed that Short class has a byteValue() method, but
    >>this method might lead to an information loss if the short value is
    >>out of the byte type range - short is 16-bits, and byte is just 8.
    >>
    >>How can I do that? How to convert a short[] to a byte[] and byte[] to
    >>short[] without loosing information?

    >
    >
    > Hrmm. Not sure if there's a better way in the Java API (it's so easy to
    > miss something), but there's a way to do it manually. Create your byte
    > array to be twice the size of your short array. For every short, enter
    > two items in the byte array: the first is the lower byte of the short,
    > and the second is the higher. When you're recoding to a short array,
    > reverse it.
    >
    > (Warning: vapor code)
    >
    > // I assume you know how bit manipulation works. Otherwise, the
    > following
    > // may or may not make sense.
    > public byte[] encodeShort(short[] s) {
    > byte[] b = new byte[s.length * 2];
    >
    > for (int ii = 0; ii < s.length; ii++) {
    > // This gets the lower byte of the short
    > // I don't know if the '& 0x00001111' is really
    > // necessary -- the conversion to byte might do
    > // that automatically -- but better safe than sorry.
    > b[ii * 2] = (byte)(s[ii] & 0x00001111);
    > // This gets the higher byte of the short
    > b[ii * 2 + 1] = (byte)((s[ii] >> 4) & 0x00001111);
    > }
    > return b;
    > }
    >
    > public short[] decodeShort(byte[] b) {
    > short[] s = new short[b.length / 2];
    >
    > for (int ii = 0; ii < s.length; ii++) {
    > // This just reverses the above. It could be broken out onto
    > // two lines for readability, if needed.
    > s[ii] = (short)(b[ii * 2] | ((short)b[ii * 2 + 1] << 4));
    > }
    > return s;
    > }
    >
    > As a final word of warning, I always get confused about whether the low
    > byte or high byte should be sent first across the network. If I'm wrong
    > on this (low byte first), I'm sure someone will step in and correct me
    > shortly. It doesn't make a difference as to whether the code works, but
    > it's good to follow standards.
    >
    > --
    > Colin McGuigan
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center