基本数据类型转换为byte数组

<span style="font-family: Arial, Helvetica, sans-serif;">/** long型转换成byte数组 */</span>
	public static byte[] longToByte(long number) { 
        byte[] b = new byte[8]; 
        longToByte(number, b, 0);
        return b; 
    }
	/** 将long型数据填充至指定byte数组 */
	public static int longToByte(long number, byte[] buf, int offset) { 
        for (int i = 0; i < 8; i++) { 
        	buf[offset+7-i] = (byte) (number & 0xff);// 将最低位保存在最低位 
            number = number >> 8; // 向右移8位 
        } 
        return offset + 8;
    } 
	/** 将byte数组转成long型数据 */
	public static long byteToLong(byte[] buf, int offset, int length) { 
        long result = 0;
		for (int i = 0; i < length; i++) { 
            result = result << 8 | (buf[i+offset]&0xff); 
        } 
        return result;
    } 

	/** int型转换成byte数组 */
	public static byte[] intToByte(int number) { 
        byte[] b = new byte[4]; 
        longToByte(number, b, 0);
        return b; 
    }
	/** 将int型数据填充至指定byte数组 */
	public static int intToByte(int number, byte[] buf, int offset) { 
        for (int i = 0; i < 4; i++) { 
        	buf[offset+3-i] = (byte) (number & 0xff);// 将最低位保存在最低位 
            number = number >> 8; // 向右移8位 
        } 
        return offset + 4;
    } 
	/** 将byte数组转成int型数据 */
	public static int byteToInt(byte[] buf, int offset, int length) { 
        int result = 0;
		for (int i = 0; i < length; i++) { 
            result = result << 8 | (buf[i+offset]&0xff); 
        } 
        return result;
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值