byte 常用转换

# Byte common transition 



**思路:**
以 int 作为例子, 首先要知道他们的换算公式。 1 int = 4 byte ,1 byte = 8 bit , 一个 bit 是一位二进制。i 和 0xFF 取与得到最低位的 1byte 数据,然后将 i 右移8位再与 0xFF 取与得到第二低位的 1byte数据,以此类推。


```java
 /**
     * 将int转换为四个字节的byte数组,高位在前,低位在后
     *
     * @param i 要转换的int参数
     *            
     * @return 四个字节的byte数组
     */
    public static byte[] intToHlBytes4(int i)
    {
        byte[] result = new byte[4];
        result[3] = (byte) (i & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[0] = (byte) ((i >> 24) & 0xFF);
        return result;
    }


    /**
     * 将int转换为四个字节的byte数组,低位在前,高位在后
     *
     * @param i 要转换的int参数
     *            
     * @return 四个字节的byte数组
     */
    public static byte[] intTolhBytes4(int i)
    {
        byte[] result = new byte[4];
        result[0] = (byte) (i & 0xFF);
        result[1] = (byte) ((i >> 8) & 0xFF);
        result[2] = (byte) ((i >> 16) & 0xFF);
        result[3] = (byte) ((i >> 24) & 0xFF);
        return result;
    }


    /**
     * 从byte数组的指定位置向后取出4位转为int数值,低位在前,高位在后
     *
     * @param bs 原始数组
     *            
     * @param startSet 开始位
     *            
     * @return
     */
    public static int hlBytesToInt(byte[] bs, int startSet)
    {
        int result;


        result = (int) ((bs[startSet] & 0xFF) | ((bs[startSet + 1] & 0xFF) << 8) | ((bs[startSet + 2] & 0xFF) << 16)
                | ((bs[startSet + 3] & 0xFF) << 24));
        return result;
    }


    /**
     * 从byte数组的指定位置向后取出4位转为int数值,高位在前,低位在后
     *
     * @param bs 原始数组
     *            
     * @param startSet 开始位
     *            
     * @return
     */
    public static int bytesToInt2(byte[] bs, int startSet)
    {
        int result;
        result = (int) (((bs[startSet] & 0xFF) << 24) | ((bs[startSet + 1] & 0xFF) << 16)
                | ((bs[startSet + 2] & 0xFF) << 8) | (bs[startSet + 3] & 0xFF));
        return result;
    }




    /**
     * long型转换为8字节的byte数组 高位在前低位在后
     *
     * @param l long数据
     *            
     * @return
     */
    public static byte[] longToHlBytes8(long l)
    {
        byte[] result = new byte[8];
        for (int i = 0; i < 8; i++)
        {
            int startSet = (result.length - 1 - i) * 8;
            result[i] = (byte) ((l >>> startSet) & 0xFF);
        }
        return result;
    }


    /**
     * short整数转换为2字节的byte数组 高位在前低位在后
     *
     * @param s short整数
     *            
     * @return
     */
    public static byte[] unsignedShortToByte2(int s)
    {
        byte[] result = new byte[2];
        result[0] = (byte) (s >> 8 & 0xFF);
        result[1] = (byte) (s & 0xFF);
        return result;
    }


    /**
     * byte数组转换为无符号short整数
     *
     * @param bs byte数组
     *            
     * @return
     */
    public static int byte2ToUnsignedShort(byte[] bs)
    {
        return byte2ToUnsignedShort(bs, 0);
    }


    /**
     * byte数组转换为无符号short整数
     *
     * @param bs byte数组
     *            
     * @param startSer 开始位置
     *            
     * @return
     */
    public static int byte2ToUnsignedShort(byte[] bs, int startSer)
    {
        int high = bs[startSer];
        int low = bs[startSer + 1];
        return (high << 8 & 0xFF00) | (low & 0xFF);
    }


    /**
     * byte数组转换为int整数
     *
     * @param bs byte数组
     *            
     * @param startSet 开始位置
     *            
     * @return int整数
     */
    public static int byte4ToInt(byte[] bs, int startSet)
    {
        int b0 = bs[startSet] & 0xFF;
        int b1 = bs[startSet + 1] & 0xFF;
        int b2 = bs[startSet + 2] & 0xFF;
        int b3 = bs[startSet + 3] & 0xFF;
        return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
    }
```


转载请注明出处:http://leonchen1024.com/2016/03/09/Byte-Common-transition/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值