Java 中short,byte数组互相转换

tech2023-03-06  73

1.byte数组转short数组 /** * byte转short * @param data * @return */ public static short[] byteToShort(byte[] data) { short[] shortValue = new short[data.length / 2]; for (int i = 0; i < shortValue.length; i++) { shortValue[i] = (short) ((data[i * 2] & 0xff) | ((data[i * 2 + 1] & 0xff) << 8)); } return shortValue; } 2.short数组转byte数组 /** * short转byte * @param data * @return */ public static byte[] shortToByte(short[] data) { byte[] byteValue = new byte[data.length * 2]; for (int i = 0; i < data.length; i++) { byteValue[i * 2] = (byte) (data[i] & 0xff); byteValue[i * 2 + 1] = (byte) ((data[i] & 0xff00) >> 8); } return byteValue; }  
最新回复(0)