欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java基本类型与字节数据之间的转换

程序员文章站 2022-05-26 15:41:50
...
public class PrimitiveTypeConverter {

	public PrimitiveTypeConverter() {

	}

	/**
	 * 获取字节数组的整型值
	 * 
	 * @param bs
	 * @return
	 */
	public static int bytesToInt(byte[] bs) {
		int intValue = 0;

		for (int i = 0; i < 4; i++) {
			intValue += (bs[i] & 0xFF) << (8 * i); // 需要反转时是(8*(3-i))
		}

		return intValue;
	}

	/**
	 * 获取字节数组的整型值
	 * 
	 * @param bs
	 * @return
	 */
	public static int bytesToIntForReverse(byte[] bs) {
		int intValue = 0;

		for (int i = 0; i < 4; i++) {
			intValue += (bs[i] & 0xFF) << (8 * (3 - i)); // 需要反转时是(8*(3-i))
		}

		return intValue;
	}

	/**
	 * 获取字节数组的整型值
	 * 
	 * @param bs
	 * @param startIndex
	 * @return
	 */
	public static int bytesToInt(byte[] bs, int startIndex) {
		int intValue = 0;

		for (int i = 0; i < 4; i++) {
			intValue += (bs[i + startIndex] & 0xFF) << (8 * i); // 需要反转时是(8*(3-i))
		}

		return intValue;
	}

	/**
	 * 获取字节数组的整型值
	 * 
	 * @param bs
	 * @param startIndex
	 * @return
	 */
	public static int bytesToIntForReserve(byte[] bs, int startIndex) {
		int intValue = 0;

		for (int i = 0; i < 4; i++) {
			intValue += (bs[i + startIndex] & 0xFF) << (8 * (3 - i)); // 需要反转时是(8*(3-i))
		}

		return intValue;
	}

	/**
	 * 获取字节数组的长整型值
	 * 
	 * @param bs
	 * @return
	 */
	public static long bytesToLong(byte[] bs) {
		long longValue = 0L;
		for (int i = 0; i < 8; i++) {
			longValue += (long) ((bs[i] & 0xFF) << (8 * i)); // 或者(8*(7-i))
			// System.out.println(longValue);
		}

		return longValue;
	}

	/**
	 * 获取字节数组的长整型值
	 * 
	 * @param bs
	 * @param startIndex
	 *            字节数组的起始位置
	 * @return
	 */
	public static long bytesToLong(byte[] bs, int startIndex) {
		long longValue = 0L;
		for (int i = 0; i < 8; i++) {
			longValue += (bs[i + startIndex] & 0xFF) << (8 * i); // 或者(8*(7-i))
		}

		return longValue;
	}

	/**
	 * 获取网络字节数组的长整型值,需反转
	 * 
	 * @param bs
	 * @param startIndex
	 *            字节数组的起始位置
	 * @return
	 */
	public static long bytesToLongForReverse(byte[] bs, int startIndex) {
		long longValue = 0L;
		for (int i = 0; i < 8; i++) {
			longValue += (long) (bs[i + startIndex] & 0xFF) << (8 * (7 - i)); // 或者(8*(7-i))
		}

		return longValue;
	}

	/**
	 * 整型转换成字节数组
	 * 
	 * @param num
	 * @return
	 */
	public static byte[] intToBytes(int num) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (num >>> (24 - i * 8));
		}
		return b;
	}

	/**
	 * 长整型转换成字节数组
	 * 
	 * @param num
	 * @return
	 */
	public static byte[] longToBytes(long num) {
		byte[] b = new byte[8];
		for (int i = 0; i < 8; i++) {
			b[i] = (byte) (num >>> (i * 8));
			// System.out.println(b[i]);
		}
		return b;
	}

	public static int bytesToIntEx(byte b[]) {
		if (b != null && b.length > 0) {
			if (b.length >= 4) {
				return b[3] & 0xff | (b[2] & 0xff) << 8 | (b[1] & 0xff) << 16
						| (b[0] & 0xff) << 24;
			} else if (b.length == 2) {
				return bytes2ToInt(b);
			} else {
				return 0;
			}

		} else {
			return 0;
		}

	}

	public static int bytes2ToInt(byte b[]) {
		return b[1] & 0xff | (b[0] & 0xff) << 8;
	}

	public static int byteToInt(byte b) {
		return (int) b;
	}

	public static int byteToUnsignedInt(byte b) {
		return b & 0xff;
	}

}