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) {

	}
}

 
 

你可能感兴趣的:(Android,java,byte,bigendian)