4

I am confused on how to convert short array to byte array. E.g. I have the follwoing short array

short[] shrt_array = new short[]{ 0x4 , 0xd7 , 0x86, 0x8c, 0xb2, 0x14, 0xc, 0x8b, 0x2d, 0x39, 0x2d, 0x2d, 0x27, 0xcb, 0x2e, 0x79, 0x46, 0x36, 0x9d , 0x62, 0x2c };

By using this link Converting short array to byte array the two methods of conversion, I get the following two different byte arrays:

 expectedByteArray = new byte[] {
    (byte) 0x4, (byte) 0xd7, (byte) 0x86, 
    (byte) 0x8c, (byte) 0xb2, (byte) 0x14,  
    (byte) 0xc, (byte) 0x8b, (byte) 0x2d,
    (byte) 0x39, (byte) 0x2d, (byte) 0x2d, 
    (byte) 0x27, (byte) 0xcb, (byte) 0x2e, 
    (byte) 0x79, (byte) 0x46, (byte) 0x36,
    (byte) 0x9d, (byte) 0x62, (byte) 0x2c,  
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0, 
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0,  
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0,  
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0,  
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0,  
    (byte) 0x0,  (byte) 0x0,  (byte) 0x0,  
    (byte) 0x0,  (byte) 0x0,  (byte)0x0};

Second result: `

expectedByteArray = new byte[] {
(byte) 0x4,  (byte) 0x0, (byte) 0xd7,  
(byte) 0x0,  (byte) 0x86,  (byte) 0x0,
(byte) 0x8c,  (byte) 0x0, (byte) 0xb2, 
(byte) 0x0,  (byte) 0x14,  (byte) 0x0, 
(byte) 0xc,  (byte) 0x0, (byte) 0x8b, 
 (byte) 0x0, (byte) 0x2d,  (byte) 0x0,
 (byte) 0x39,  (byte) 0x0, (byte) 0x2d, 
 (byte) 0x0, (byte) 0x2d,  (byte) 0x0, 
(byte) 0x27,  (byte) 0x0, (byte) 0xcb, 
 (byte) 0x0, (byte) 0x2e,  (byte) 0x0, 
(byte) 0x79,  (byte) 0x0, (byte) 0x46, 
 (byte) 0x0, (byte) 0x36,  (byte) 0x0,
(byte) 0x9d,  (byte) 0x0, (byte) 0x62,  
(byte) 0x0, (byte) 0x2c,  (byte) 0x0};

`

Can you help me which is the right one.

Community
  • 1
  • 1
student
  • 53
  • 1
  • 7
  • 1
    What do you mean by "right"? It depends on your requirements. Do you need to convert each `short` into a single `byte` (e.g. by ignoring the top 8 bits) or into two `byte` values? – Jon Skeet Oct 25 '13 at 08:34
  • I have to convert each short into byte. Every value of short array into byte array – student Oct 25 '13 at 08:35
  • 1
    What john is pointing out is that a short is two bytes. – tom Oct 25 '13 at 08:40
  • see http://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java?rq=1 – tom Oct 25 '13 at 08:40
  • A Short is 2 bytes wide, so your answer does not give the information, Jon asked for. You can convert either ignoring the top byte of the short, or by adding both bytes to the byte array. Which one do you need? – Fildor Oct 25 '13 at 08:41
  • It is for me clear that a short is two bytes. Link that I posted they allocate a byte array 2*short_array.length. Which gives also a double size in compare to short_array. But what I do not udesrtand is, whcih shuld I consider as a corret byte array. – student Oct 25 '13 at 08:45
  • @Fildor, i need the second method, adding both bytes to the byte array. – student Oct 25 '13 at 08:47
  • just a side note, variable names should be in camelCase. `shrt_array` should be shrtArray. – Nishant Oct 25 '13 at 09:02
  • 1
    @student - In what order? Most significant byte first, or least significant byte first? – Ingo Oct 25 '13 at 10:06
  • @Ingo, was it possible two explain or provide some code for both orders. This means MSB first and LSb first? – student Oct 28 '13 at 05:44
  • @student On your request, wrote an answer. – Ingo Oct 28 '13 at 09:38

2 Answers2

10

Your use of asShortBuffer was somewhat close. It should be:

ByteBuffer buffer = ByteBuffer.allocate(shrt_array.length * 2);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.asShortBuffer().put(shrt_array);
byte[] bytes = buffer.array();
VGR
  • 40,506
  • 4
  • 48
  • 63
5

Doing it manually for explicit control of byte order:

byte[] tobytes(short[] shorts, boolean bigendian) {
    int n = 0;
    byte[] bytes = new byte[2*shorts.length];

    for (n=0; n < shorts.length; n++) {
        byte lsb = shorts[n] & 0xff;
        byte msb = (shorts[n] >> 8) & 0xff;
        if (bigendian) {
            bytes[2*n]   = msb;
            bytes[2*n+1] = lsb;
        } else {
            bytes[2*n]   = lsb;
            bytes[2*n+1] = msb;
        }
    }
    return bytes;
}

If s is the short value, you have least significant byte with s & 0xff and most significant byte as (s >> 8) & 0xff. You can put them at byte array index 2*n and 2*n+1 in the order you want, where n is the index in the short array.

Ingo
  • 36,037
  • 5
  • 53
  • 100