Java 基本數據類型與byte數組互轉

package com.vcc.convert;

/**
 * BigEndian和LittleEndian轉換,BigEndian數組下標0表示最高位
 * 
 * @author proxyme
 * 
 */
public class EndianConverter {
	/**
	 * 將LittleEndian轉換爲BigEndian
	 * 
	 * @param bytes
	 * @return
	 */
	public static byte[] littleToBig(byte[] bytes) {
		if (bytes == null) {
			throw new NullPointerException();
		}
		byte[] temp = new byte[bytes.length];
		for (int i = bytes.length - 1; i >= 0; i--) {
			temp[i] = bytes[bytes.length - 1 - i];
		}
		return temp;
	}

	/**
	 * 將BigEndian轉換爲LittleEndian
	 * 
	 * @param bytes
	 * @return
	 */
	public static byte[] bigToLittle(byte[] bytes) {
		return littleToBig(bytes);
	}

	/**
	 * 將只包含一個byte的數組轉換爲byte.
	 * 
	 * @param bytes
	 * @return
	 */
	public static byte toByte(byte[] bytes) {
		return bytes[0];
	}

	/**
	 * 將Java byte轉換爲Java char.
	 * 
	 * @param b
	 * @return
	 */
	public static char toChar(byte b) {
		return (char) (b & 0xff);
	}

	/**
	 * 將Java char轉換爲byte數組.
	 * 
	 * @param ch
	 * @return
	 */
	public static byte[] toBytes(char ch) {
		byte[] bytes = new byte[2];
		bytes[0] = (byte) (ch >> 8 & 0xff);
		bytes[1] = (byte) (ch & 0xff);
		return bytes;
	}

	public static byte toLittleBytes(char ch) {

		return (byte) ch;
	}

	/**
	 * 將Java byte數組轉換爲Java char.
	 * 
	 * @param bytes
	 * @return
	 */
	public static char toChar(byte[] bytes) {
		return (char) ((bytes[0] << 8 & 0xff00) | (bytes[1] & 0xff));
	}

	/**
	 * 將srcPos開始的Java 2個字節轉換爲Java char.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static char toChar(byte[] bytes, int srcPos) {
		return (char) ((bytes[srcPos] << 8 & 0xff00) | (bytes[srcPos + 1] & 0xff));
	}

	/**
	 * 將Java byte數組轉換爲char數組,byte數組大小必須爲2的倍數
	 * 
	 * @param b
	 * @return
	 */
	public static char[] toCharArray(byte[] b) {
		char[] chs = new char[b.length];
		int offset = 0;
		for (int i = 0; i < b.length; i++) {
			chs[i] = toChar(b, offset);
			offset += 2;
		}
		return chs;
	}

	/**
	 * 將Java byte數組轉換爲char數組.第一個字節從srcPos開始。
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static char[] toCharArray(byte[] bytes, int srcPos) {
		char[] chs = new char[(bytes.length - srcPos) >> 1];
		int offset = 0;
		for (int i = 0; i < chs.length; i++) {
			chs[i] = toChar(bytes, srcPos + offset);
			offset += 2;
		}
		return chs;
	}

	/**
	 * 將Java byte數組轉換爲char數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 *            第一個字節位置
	 * @param charCount
	 *            轉換爲char的個數
	 * @return
	 */
	public static char[] toCharArray(byte[] bytes, int srcPos, int charCount) {
		char[] chs = new char[charCount];
		int offset = 0;
		for (int i = 0; i < charCount; i++) {
			chs[i] = toChar(bytes, srcPos + offset);
			offset += 2;
		}
		return chs;
	}

	/**
	 * 將LittleEndian byte轉換爲BigEndian char.(32位平臺上c/c++
	 * char,爲一個字節,相當於Java的byte,unsigned char範圍爲0~255,所以unsigned
	 * char要用Java的short來表示)
	 * 
	 * @param bytes
	 * @return
	 * @see #unsignedCharToShort(byte)
	 */
	public static char littleToChar(byte b) {
		return (char) (b & 0xff);
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian char數組.(32位平臺上c/c++
	 * char爲一個字節,相當於Java的byte,unsigned char範圍爲0~255,所以unsigned
	 * char要用Java的short來表示)
	 * 
	 * @param bytes
	 * @return
	 * @see #unsignedCharToShort(byte)
	 */
	public static char[] littleToCharArray(byte[] bytes) {
		char[] charArray = new char[bytes.length];
		for (int i = 0; i < charArray.length; i++) {
			charArray[i] = littleToChar(bytes[i]);
		}
		return charArray;
	}

	/**
	 * * 將LittleEndian byte數組轉換爲BigEndian char數組.(32位平臺上c/c++
	 * char爲一個字節,相當於Java的byte,unsigned char範圍爲0~255,所以unsigned
	 * char要用Java的short來表示)
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static char[] littleToCharArray(byte[] bytes, int srcPos, int count) {
		char[] charArray = new char[count];
		for (int i = 0; i < count; i++) {
			charArray[i] = littleToChar(bytes[srcPos + i]);
		}
		return charArray;
	}

	/**
	 * 將Java int轉換爲byte數組.
	 * 
	 * @param i
	 * @return
	 */
	public static byte[] toBytes(int i) {
		byte[] bytes = new byte[4];
		bytes[0] = (byte) (i >> 24 & 0xff);
		bytes[1] = (byte) (i >> 16 & 0xff);
		bytes[2] = (byte) (i >> 8 & 0xff);
		bytes[3] = (byte) (i & 0xff);
		return bytes;
	}

	public static byte[] toLittleBytes(int i) {
		byte[] bytes = new byte[4];
		bytes[3] = (byte) (i >> 24 & 0xff);
		bytes[2] = (byte) (i >> 16 & 0xff);
		bytes[1] = (byte) (i >> 8 & 0xff);
		bytes[0] = (byte) (i & 0xff);
		return bytes;
	}

	/**
	 * 將 Java int轉換爲unsigned short
	 * 
	 * @param i
	 * @return
	 */
	public static byte[] toUnsignedShort(int i) {
		byte[] bytes = new byte[2];
		bytes[1] = (byte) (i >> 8 & 0xff);
		bytes[0] = (byte) (i & 0xff);
		return bytes;
	}
	/**
	 * 將Java 數組轉換爲Java int.
	 * 
	 * @param bytes
	 * @return
	 */
	public static int toInt(byte[] bytes) {
		int rt = 0;
		rt += (bytes[0] & 0xff) << 24;
		rt += (bytes[1] & 0xff) << 16;
		rt += (bytes[2] & 0xff) << 8;
		rt += (bytes[3] & 0xff);
		return rt;
	}

	/**
	 * 將以srcPos開始的Java 4個字節轉換爲Java int.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static int toInt(byte[] bytes, int srcPos) {
		int rt = 0;
		rt += (bytes[srcPos] & 0xff) << 24;
		rt += (bytes[srcPos + 1] & 0xff) << 16;
		rt += (bytes[srcPos + 2] & 0xff) << 8;
		rt += (bytes[srcPos + 3] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian unsigned int(32位平臺c/c++ unsigned int)轉換爲Java long.
	 * (32位平臺c/c++ unsigned int要用Java的long來表示)
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static long unsignedIntToLong(byte[] bytes, int srcPos) {
		long rt = 0;
		rt += (long) (bytes[srcPos + 3] & 0xff) << 24;
		rt += (long) (bytes[srcPos + 2] & 0xff) << 16;
		rt += (long) (bytes[srcPos + 1] & 0xff) << 8;
		rt += (long) (bytes[srcPos] & 0xff);
		return rt;
	}

	/**
	 * 將Java byte數組轉換爲Java int數組
	 * 
	 * @param bytes
	 * @return
	 */
	public static int[] toIntArray(byte[] bytes) {
		int[] intArray = new int[bytes.length >> 2];
		int offset = 0;
		for (int i = 0; i < intArray.length; i++) {
			intArray[i] = toInt(bytes, offset);
			offset += 4;
		}
		return intArray;
	}

	/**
	 * 將Java byte數組轉換爲Java int數組.第一個字節從srcPos開始,count指定要轉換的int個數
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static int[] toIntArray(byte[] bytes, int srcPos, int count) {
		int[] intArray = new int[count];
		int offset = 0;
		for (int i = 0; i < count; i++) {
			intArray[i] = toInt(bytes, offset + srcPos);
			offset += 4;
		}
		return intArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian int數組.
	 * 
	 * @param bytes
	 * @return
	 */
	public static int[] littleToIntArray(byte[] bytes) {
		int[] intArray = new int[bytes.length >> 2];
		int offset = 0;
		for (int i = 0; i < intArray.length; i++) {
			intArray[i] = 0;
			intArray[i] += (bytes[offset + 3] & 0xff) << 24;
			intArray[i] += (bytes[offset + 2] & 0xff) << 16;
			intArray[i] += (bytes[offset + 1] & 0xff) << 8;
			intArray[i] += (bytes[offset + 0] & 0xff);
			offset += 4;
		}
		return intArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian int數組.第一個字節從srcPos開始,count爲轉換爲int的個數.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static int[] littleToIntArray(byte[] bytes, int srcPos, int count) {
		int[] intArray = new int[count];
		int offset = 0;
		for (int i = 0; i < count; i++) {
			intArray[i] = 0;
			int index = offset + srcPos;
			intArray[i] += (bytes[index + 3] & 0xff) << 24;
			intArray[i] += (bytes[index + 2] & 0xff) << 16;
			intArray[i] += (bytes[index + 1] & 0xff) << 8;
			intArray[i] += (bytes[index + 0] & 0xff);
			offset += 4;
		}
		return intArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian int.
	 * 
	 * @param bytes
	 * @return
	 */
	public static int littleToInt(byte[] bytes) {
		int rt = 0;
		rt += (bytes[3] & 0xff) << 24;
		rt += (bytes[2] & 0xff) << 16;
		rt += (bytes[1] & 0xff) << 8;
		rt += (bytes[0] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian 4個byte轉換爲BigEndian int.第一個字節從srcPos開始.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static int littleToInt(byte[] bytes, int srcPos) {
		int rt = 0;
		rt += (bytes[srcPos + 3] & 0xff) << 24;
		rt += (bytes[srcPos + 2] & 0xff) << 16;
		rt += (bytes[srcPos + 1] & 0xff) << 8;
		rt += (bytes[srcPos] & 0xff);
		return rt;
	}

	/**
	 * 將Java short轉換爲Java byte數組.
	 * 
	 * @param s
	 * @return
	 */
	public static byte[] toBytes(short s) {
		byte[] bytes = new byte[2];
		bytes[0] = (byte) (s >> 8 & 0xff);
		bytes[1] = (byte) (s & 0xff);
		return bytes;
	}

	public static byte[] toLittleBytes(short s) {
		byte[] bytes = new byte[2];
		bytes[1] = (byte) (s >> 8 & 0xff);
		bytes[0] = (byte) (s & 0xff);
		return bytes;
	}

	/**
	 * 將Java short轉換爲unsigned char
	 * 
	 * @param s
	 * @return
	 */
	public static byte toUnsignedChar(short s) {
		return (byte) s;
	}

	/**
	 * 將Java byte數組轉換爲Java short.
	 * 
	 * @param bytes
	 * @return
	 */
	public static short toShort(byte[] bytes) {
		short rt = 0;
		rt += (bytes[0] << 8 & 0xff00);
		rt += (bytes[1] & 0xff);
		return rt;
	}

	/**
	 * 將從srcPos開始的2個Java字節轉換爲Java short.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static short toShort(byte[] bytes, int srcPos) {
		short rt = 0;
		rt += (bytes[srcPos] << 8 & 0xff00);
		rt += (bytes[srcPos + 1] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian 2個字節(c/c++ unsigned short)轉換爲BitEndian int.
	 * 
	 * @param bytes
	 * @return
	 */
	public static int unsignedShortToInt(byte[] bytes) {
		int rt = 0;
		rt += (int) (bytes[1] & 0xff) << 8;
		rt += (int) (bytes[0] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian 2個字節(c/c++ unsigned short)轉換爲BitEndian
	 * int.srcPos指定第一個字節開始位置.(c/c++中unsigned short對應Java中int)
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static int unsignedShortToInt(byte[] bytes, int srcPos) {
		int rt = 0;
		rt += (int) (bytes[srcPos + 1] & 0xff) << 8;
		rt += (int) (bytes[srcPos] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian unsigned char(例如c/c++中 unsigned char =
	 * 255,使用socket發到Java時得到的byte = -1,轉換後可以得到正確值255)
	 * 
	 * @param b
	 * @return
	 */
	public static short unsignedCharToShort(byte b) {
		return (short) (b & 0xff);
	}

	/**
	 * 將Java byte數組轉換爲Java short數組
	 * 
	 * @param bytes
	 * @return
	 */
	public static short[] toShortArray(byte[] bytes) {
		short[] shortArray = new short[bytes.length >> 1];
		int offset = 0;
		for (int i = 0; i < shortArray.length; i++) {
			shortArray[i] = toShort(bytes, offset);
			offset += 4;
		}
		return shortArray;
	}

	/**
	 * 將Java byte數組轉換爲Java short數組,srcPos指定開始位置,count爲short個數.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static short[] toShortArray(byte[] bytes, int srcPos, int count) {
		short[] shortArray = new short[count];
		int offset = 0;
		for (int i = 0; i < count; i++) {
			shortArray[i] = toShort(bytes, srcPos + offset);
			offset += 4;
		}
		return shortArray;
	}

	/**
	 * 將LittleEndian 2個字節byte(c/c++ short)轉換爲BitEndian short.(2個字節轉換爲一個short);
	 * 
	 * @param bytes
	 * @return
	 */
	public static short littleToShort(byte[] bytes) {
		short rt = 0;
		rt += (bytes[1] << 8 & 0xff00);
		rt += (bytes[0] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian 2個字節(c/c++ short)轉換爲BigEndian short.srcPos指定開始位置.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static short littleToShort(byte[] bytes, int srcPos) {
		short rt = 0;
		rt += (bytes[srcPos + 1] << 8 & 0xff00);
		rt += (bytes[srcPos] & 0xff);
		return rt;
	}

	/**
	 * 將LittleEndian byte數組(c/c++ short)轉換爲BigEndian short數組.
	 * 
	 * @param bytes
	 * @return
	 */
	public static short[] littleToShortArray(byte[] bytes) {
		short[] shortArray = new short[bytes.length >> 1];
		int offset = 0;
		for (int i = 0; i < shortArray.length; i++) {
			shortArray[i] = littleToShort(bytes, offset);
			offset += 4;
		}
		return shortArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian short數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static short[] littleToShortArray(byte[] bytes, int srcPos, int count) {
		short[] shortArray = new short[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			shortArray[i] = littleToShort(bytes, offset);
			offset += 4;
		}
		return shortArray;
	}

	/**
	 * 將Java long轉換爲Java byte數組.Java long爲8個字節.
	 * 
	 * @param l
	 * @return
	 */
	public static byte[] toBytes(long l) {
		byte[] bytes = new byte[8];
		bytes[0] = (byte) (l >> 56 & 0xff);
		bytes[1] = (byte) (l >> 48 & 0xff);
		bytes[2] = (byte) (l >> 40 & 0xff);
		bytes[3] = (byte) (l >> 32 & 0xff);
		bytes[4] = (byte) (l >> 24 & 0xff);
		bytes[5] = (byte) (l >> 16 & 0xff);
		bytes[6] = (byte) (l >> 8 & 0xff);
		bytes[7] = (byte) (l >> 0 & 0xff);
		return bytes;
	}

	public static byte[] toLittleBytes(long l) {
		byte[] bytes = new byte[4];
		bytes[3] = (byte) (l >> 24 & 0xff);
		bytes[2] = (byte) (l >> 16 & 0xff);
		bytes[1] = (byte) (l >> 8 & 0xff);
		bytes[0] = (byte) (l >> 0 & 0xff);
		return bytes;
	}

	public static byte[] toUnsignedLong(long l) {
		return toLittleBytes(l);
	}

	/**
	 * 將Java byte數組轉換爲Java long.
	 * 
	 * @param bytes
	 * @return
	 */
	public static long toLong(byte[] bytes) {
//因爲默認運算時類型爲int,導致m = byte[x]&0xff << n ,n>=32時超出int範圍,所以m = 0,所以一定要先轉換爲long
		return (((long) bytes[0] & 0xff) << 56)
				| (((long) bytes[1] & 0xff) << 48)
				| (((long) bytes[2] & 0xff) << 40)
				| (((long) bytes[3] & 0xff) << 32)
				| (((long) bytes[4] & 0xff) << 24)
				| (((long) bytes[5] & 0xff) << 16)
				| (((long) bytes[6] & 0xff) << 8)
				| (((long) bytes[7] & 0xff) << 0);
	}

	/**
	 * 將srcPos開始的8個byte轉換爲Java long.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static long toLong(byte[] bytes, int srcPos) {
		return (((long) bytes[srcPos] & 0xff) << 56)
				| (((long) bytes[srcPos + 1] & 0xff) << 48)
				| (((long) bytes[srcPos + 2] & 0xff) << 40)
				| (((long) bytes[srcPos + 3] & 0xff) << 32)
				| (((long) bytes[srcPos + 4] & 0xff) << 24)
				| (((long) bytes[srcPos + 5] & 0xff) << 16)
				| (((long) bytes[srcPos + 6] & 0xff) << 8)
				| (((long) bytes[srcPos + 7] & 0xff) << 0);
	}

	/**
	 * 將byte數組轉換爲long數組.
	 * 
	 * @param bytes
	 * @return
	 */
	public static long[] toLongArray(byte[] bytes) {
		long[] longArray = new long[bytes.length >> 3];
		int offset = 0;
		for (int i = 0; i < longArray.length; i++) {
			longArray[i] = toLong(bytes, offset);
			offset += 8;
		}
		return longArray;
	}

	/**
	 * 將Java byte數組轉換爲Java long數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static long[] toLongArray(byte[] bytes, int srcPos, int count) {
		long[] longArray = new long[count];
		int offset = srcPos;
		for (int i = 0; i < longArray.length; i++) {
			longArray[i] = toLong(bytes, offset);
			offset += 8;
		}
		return longArray;
	}

	/**
	 * 將LittleEndian byte數組(c/c++ long)轉換爲BigEndian long.(32位平臺c/c++
	 * long爲4個字節,而Java的long爲8個字節).
	 * 
	 * @param bytes
	 * @return
	 */
	public static long littleToLong(byte[] bytes) {
		return ((bytes[3] & 0xff) << 24) | ((bytes[2] & 0xff) << 16)
				| ((bytes[1] & 0xff) << 8) | ((bytes[0] & 0xff) << 0);
	}

	/**
	 * 將LittleEndian byte數組(c/c++ long)轉換爲BigEndian long.(32位平臺c/c++
	 * long爲4個字節,而Java的long爲8個字節).
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static long littleToLong(byte[] bytes, int srcPos) {
		return ((bytes[srcPos + 3] & 0xff) << 24)
				| ((bytes[srcPos + 2] & 0xff) << 16)
				| ((bytes[srcPos + 1] & 0xff) << 8)
				| ((bytes[srcPos] & 0xff) << 0);
	}

	public static long littleBytesToLong(byte[] bytes, int srcPos) {
		return (((long) bytes[srcPos + 7] & 0xff) << 56)
				| (((long) bytes[srcPos + 6] & 0xff) << 48)
				| (((long) bytes[srcPos + 5] & 0xff) << 40)
				| (((long) bytes[srcPos + 4] & 0xff) << 32)
				| (((long) bytes[srcPos + 3] & 0xff) << 24)
				| (((long) bytes[srcPos + 2] & 0xff) << 16)
				| (((long) bytes[srcPos + 1] & 0xff) << 8)
				| (((long) bytes[srcPos] & 0xff) << 0);
	}

	/**
	 * 將LittleEndian byte數組(c/c++ unsigned long)轉換爲BigEndian long.(32位平臺c/c++
	 * long爲4個字節,而Java的long爲8個字節).
	 * 
	 * @param bytes
	 * @return
	 */
	public static long unsignedLongToLong(byte[] bytes) {
		return (((long) bytes[3] & 0xff) << 24)
				| (((long) bytes[2] & 0xff) << 16)
				| (((long) bytes[1] & 0xff) << 8)
				| (((long) bytes[0] & 0xff) << 0);
	}

	/**
	 * 將LittleEndian byte數組(c/c++ unsigned long)轉換爲BigEndian long.(32位平臺c/c++
	 * long爲4個字節,而Java的long爲8個字節).
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static long unsignedLongToLong(byte[] bytes, int srcPos) {
		return (((long) bytes[srcPos + 3] & 0xff) << 24)
				| (((long) bytes[srcPos + 2] & 0xff) << 16)
				| (((long) bytes[srcPos + 1] & 0xff) << 8)
				| (((long) bytes[srcPos] & 0xff) << 0);
	}

	/**
	 * 將LittleEndian byte數組(c/c++ long)轉換爲BigEndian long.(32位平臺c/c++
	 * long爲4個字節,而Java的long爲8個字節).
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static long[] littleToLongArray(byte[] bytes, int srcPos, int count) {
		long[] longArray = new long[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			longArray[i] = littleToLong(bytes, offset);
			offset += 8;
		}
		return longArray;
	}

	/**
	 * 將Java float轉換爲Java byte數組
	 * 
	 * @param f
	 * @return
	 */
	public static byte[] toBytes(float f) {
		return toBytes(Float.floatToIntBits(f));
	}

	public static byte[] toLittleBytes(float f) {
		return toLittleBytes(Float.floatToIntBits(f));
	}

	/**
	 * 將Java byte數組轉換爲Java float.
	 * 
	 * @param bytes
	 * @return
	 */
	public static float toFloat(byte[] bytes) {
		return Float.intBitsToFloat(toInt(bytes));
	}

	/**
	 * 將Java byte數組轉換爲Java float.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static float toFloat(byte[] bytes, int srcPos) {
		return Float.intBitsToFloat(toInt(bytes, srcPos));
	}

	/**
	 * 將byte數組轉換爲Java float數組
	 * 
	 * @param bytes
	 * @return
	 */
	public static float[] toFloatArray(byte[] bytes) {
		float[] floatArray = new float[bytes.length >> 2];
		int offset = 0;
		for (int i = 0; i < floatArray.length; i++) {
			floatArray[i] = toFloat(bytes, offset);
			offset += 4;
		}
		return floatArray;
	}

	/**
	 * 將Java byte數組轉換爲Java float數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static float[] toFloatArray(byte[] bytes, int srcPos, int count) {
		float[] floatArray = new float[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			floatArray[i] = toFloat(bytes, offset);
			offset += 4;
		}
		return floatArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian float.
	 * 
	 * @param bytes
	 * @return
	 */
	public static float littleToFloat(byte[] bytes) {
		return toFloat(littleToBig(bytes));
	}

	/**
	 * 將LittleEndian轉換爲BigEndian float.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static float littleToFloat(byte[] bytes, int srcPos) {
		return Float.intBitsToFloat(littleToInt(bytes, srcPos));
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian float數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static float[] littleToFlotArray(byte[] bytes, int srcPos, int count) {
		float[] floatArray = new float[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			floatArray[i] = littleToFloat(bytes, offset);
			offset += 4;
		}
		return floatArray;
	}

	/**
	 * 將Java double轉換爲Java byte數組.
	 * 
	 * @param d
	 * @return
	 */
	public static byte[] toBytes(double d) {
		return toBytes(Double.doubleToLongBits(d));
	}

	public static byte[] toLittleBytes(double d) {
		return toLittleBytes(Double.doubleToLongBits(d));
	}

	/**
	 * 將Java byte數組轉換爲Java double.
	 * 
	 * @param bytes
	 * @return
	 */
	public static double toDouble(byte[] bytes) {
		return Double.longBitsToDouble(toLong(bytes));
	}

	/**
	 * 將Java byte數組轉換爲Java double.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static double toDouble(byte[] bytes, int srcPos) {
		return Double.longBitsToDouble(toLong(bytes, srcPos));
	}

	/**
	 * 將Java byte數組轉換爲Java double數組
	 * 
	 * @param bytes
	 * @return
	 */
	public static double[] toDoubleArray(byte[] bytes) {
		double[] doubleArray = new double[bytes.length >> 3];
		int offset = 0;
		for (int i = 0; i < doubleArray.length; i++) {
			doubleArray[i] = toDouble(bytes, offset);
			offset += 8;
		}
		return doubleArray;
	}

	/**
	 * 將Java byte數組轉換爲Java double數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static double[] toDoubleArray(byte[] bytes, int srcPos, int count) {
		double[] doubleArray = new double[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			doubleArray[i] = toDouble(bytes, offset);
			offset += 8;
		}
		return doubleArray;
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian double.
	 * 
	 * @param bytes
	 * @return
	 */
	public static double littleToDouble(byte[] bytes) {
		return toDouble(littleToBig(bytes));
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian double.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @return
	 */
	public static double littleToDouble(byte[] bytes, int srcPos) {
		return Double.longBitsToDouble(littleBytesToLong(bytes, srcPos));
	}

	/**
	 * 將LittleEndian byte數組轉換爲BigEndian double數組.
	 * 
	 * @param bytes
	 * @param srcPos
	 * @param count
	 * @return
	 */
	public static double[] littleToDoubleArray(byte[] bytes, int srcPos,
			int count) {
		double[] doubleArray = new double[count];
		int offset = srcPos;
		for (int i = 0; i < count; i++) {
			doubleArray[i] = littleToDouble(bytes, offset);
			offset += 8;
		}
		return doubleArray;
	}

	public static void main(String[] arg) {

	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章