A cleaner, albeit far less efficient solution is:
ByteBuffer buffer = ByteBuffer.allocate(2);
buffer.putShort(value);
return buffer.array();
Keep this in mind when you have to do more complex byte transformations in the future. ByteBuffers are very powerful.
//---------------------------------------------------------------------------------------------------------------------------------
总结:这里可以看到使用传统的方法就是移位取值。
还可以看到使用比较新的方案ByteBuffer来做,这个ByteBuffer不仅仅可以很方便的实现short转化为
byte[],还可以很方便的实现int转化为byte[]等等。