位操作

/**
 * 位操作
*/
	/**
	 * 从指定字节数组中读取boolean值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return boolean值
	 */
	public static boolean getBoolean(byte[] b, int off) {
		return b[off] != 0;
	}

	/**
	 * 从指定字节数组中读取char值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return char值
	 */
	public static char getChar(byte[] b, int off) {
		return (char) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
	}

	/**
	 * 从指定字节数组中读取short值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return short值
	 */
	public static short getShort(byte[] b, int off) {
		return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
	}

	/**
	 * 从指定字节数组中读取int值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return int值
	 */
	public static int getInt(byte[] b, int off) {
		return ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
				+ ((b[off + 1] & 0xFF) << 16) + ((b[off + 0] & 0xFF) << 24);
	}

	/**
	 * 从指定字节数组中读取float值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return float值
	 */
	public static float getFloat(byte[] b, int off) {
		int i = ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
				+ ((b[off + 1] & 0xFF) << 16) + ((b[off + 0] & 0xFF) << 24);
		return Float.intBitsToFloat(i);
	}

	/**
	 * 从指定字节数组中读取long值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return long值
	 */
	public static long getLong(byte[] b, int off) {
		return ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8)
				+ ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24)
				+ ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40)
				+ ((b[off + 1] & 0xFFL) << 48) + ((b[off + 0] & 0xFFL) << 56);
	}

	/**
	 * 从指定字节数组中读取double值.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            读取的开始字节位置
	 * @return double值
	 */
	public static double getDouble(byte[] b, int off) {
		long j = ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8)
				+ ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24)
				+ ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40)
				+ ((b[off + 1] & 0xFFL) << 48) + ((b[off + 0] & 0xFFL) << 56);
		return Double.longBitsToDouble(j);
	}

	/**
	 * 将boolean值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            boolean值
	 */
	public static void putBoolean(byte[] b, int off, boolean val) {
		b[off] = (byte) (val ? 1 : 0);
	}

	/**
	 * 将char值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            char值
	 */
	public static void putChar(byte[] b, int off, char val) {
		b[off + 1] = (byte) (val >>> 0);
		b[off + 0] = (byte) (val >>> 8);
	}

	/**
	 * 将short值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            short值
	 */
	public static void putShort(byte[] b, int off, short val) {
		b[off + 1] = (byte) (val >>> 0);
		b[off + 0] = (byte) (val >>> 8);
	}

	/**
	 * 将int值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            int值
	 */
	public static void putInt(byte[] b, int off, int val) {
		b[off + 3] = (byte) (val >>> 0);
		b[off + 2] = (byte) (val >>> 8);
		b[off + 1] = (byte) (val >>> 16);
		b[off + 0] = (byte) (val >>> 24);
	}

	/**
	 * 将float值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            float值
	 */
	public static void putFloat(byte[] b, int off, float val) {
		int i = Float.floatToIntBits(val);
		b[off + 3] = (byte) (i >>> 0);
		b[off + 2] = (byte) (i >>> 8);
		b[off + 1] = (byte) (i >>> 16);
		b[off + 0] = (byte) (i >>> 24);
	}

	/**
	 * 将long值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            long值
	 */
	public static void putLong(byte[] b, int off, long val) {
		b[off + 7] = (byte) (val >>> 0);
		b[off + 6] = (byte) (val >>> 8);
		b[off + 5] = (byte) (val >>> 16);
		b[off + 4] = (byte) (val >>> 24);
		b[off + 3] = (byte) (val >>> 32);
		b[off + 2] = (byte) (val >>> 40);
		b[off + 1] = (byte) (val >>> 48);
		b[off + 0] = (byte) (val >>> 56);
	}

	/**
	 * 将double值写入指定字节数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param off
	 *            开始写入的字节位置
	 * @param val
	 *            double值
	 */
	public static void putDouble(byte[] b, int off, double val) {
		long j = Double.doubleToLongBits(val);
		b[off + 7] = (byte) (j >>> 0);
		b[off + 6] = (byte) (j >>> 8);
		b[off + 5] = (byte) (j >>> 16);
		b[off + 4] = (byte) (j >>> 24);
		b[off + 3] = (byte) (j >>> 32);
		b[off + 2] = (byte) (j >>> 40);
		b[off + 1] = (byte) (j >>> 48);
		b[off + 0] = (byte) (j >>> 56);
	}

	/**
	 * 从指定字节数组中读取int值.<br>
	 * 如果数组长度不足4位,会自动补足4位.
	 * 
	 * @param b
	 *            字节数组
	 * @return int值
	 */
	public static int getInt(byte[] b) {
		// 补成长度为4字节的字节数组
		b = ensureCapacity(b, 4);
		return getInt(b, 0);
	}

	/**
	 * 将int值写入byte数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param int值
	 */
	public static void putInt(byte[] b, int value) {
		byte[] tmp = new byte[4];
		putInt(tmp, 0, value);
		System.arraycopy(tmp, tmp.length - b.length, b, 0, b.length);
	}

	/**
	 * 从指定字节数组中读取long值.<br>
	 * 如果数组长度不足8位,会自动补足8位.
	 * 
	 * @param b
	 *            字节数组
	 * @return long值
	 */
	public static long getLong(byte[] b) {
		// 补成长度为8字节的字节数组
		b = ensureCapacity(b, 8);
		return getLong(b, 0);
	}

	/**
	 * 将long值写入byte数组.<br>
	 * 
	 * @param b
	 *            字节数组
	 * @param long值
	 */
	public static void putLong(byte[] b, long value) {
		byte[] tmp = new byte[8];
		putLong(tmp, 0, value);
		System.arraycopy(tmp, tmp.length - b.length, b, 0, b.length);
	}

	private static byte[] ensureCapacity(byte[] b, int capacity) {
		if (b.length > capacity) {
			return b;
		}
		byte[] newB = new byte[capacity];
		System.arraycopy(b, 0, newB, capacity - b.length, b.length);
		return newB;
	}

	/**
	 * 根据报文头长度和报文体长度获取报文体报文头,未达到指定长度补零
	 * 
	 * @return
	 */
	public static String getVarMsgHeader(int varMsgHeaderLength, int bodyLength) {
		String t = "";
		for (int i = 0; i < varMsgHeaderLength
				- String.valueOf(bodyLength).length(); i++) {
			t += "0";
		}
		return t + bodyLength;
	}

	

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值