I have a
short val = 150;
how do I put this short into a byte[2]?
I have found some code examples because other people asked these question too, but none of them worked for me well.. please help me
I have a
short val = 150;
how do I put this short into a byte[2]?
I have found some code examples because other people asked these question too, but none of them worked for me well.. please help me
Try this.
short val = 150;
byte[] result = ByteBuffer.allocate(2).putShort(val).array();
You can do this:
short val = 150;
byte []array = new byte[2]
array[0] = (byte)(val & 0xff);
array[1] = (byte)((val >> 8) & 0xff);
You are putting in array[0] the least significant byte. In array[1] the most significant byte.
For more about the meaning of the operations you can read here