Java examples for java.lang:byte Array Convert
Converts an array of bytes to a short.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { byte data = 2; System.out.println(bytesToShort(data)); }//from w w w . ja v a2s. com /** * <p> * Converts an array of bytes to a short. If there are more than four bytes, the first four bytes in the array are used and the * rest are ignored.<br> * </p> * * @param data * The byte array to be converted to a short. * @return The short equivalent of the given byte array. */ public static short bytesToShort(byte... data) { byte[] tmp = data; if (data.length < 2) { tmp = new byte[] { data[0], 0 }; } return (short) ((tmp[1] & 0xFF) << 8 | tmp[0] & 0xFF); } }