Convert short to byte array in Java
Description
The following code shows how to convert short to byte array.
Example
/*from w w w . j a v a 2 s . co m*/
import java.nio.ByteBuffer;
public class Main {
public static byte[] convertToByteArray(short value) {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.putShort(value);
return buffer.array();
}
public static void main(String[] argv) {
System.out.println(convertToByteArray((short)213));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »