Java 中 short, byte 数组互相转换

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;
	}
 

猜你喜欢

转载自blog.csdn.net/u011193452/article/details/108382839