Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; public class Main { public static byte[] shortToByteArray(short number) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeShort(number); dos.flush(); // lenth have to be 2 byte byte[] d = bos.toByteArray(); dos.close(); return d; } catch (IOException e) { throw new RuntimeException(e); } } }