byte 常用转换

来源:互联网 发布:剑三喵太捏脸数据 编辑:程序博客网 时间:2024/04/30 16:34
<span style="font-size:18px;">一些常用的bytes转换 。 写着备用 /**     * 将int转换为四个字节的byte数组,高位在前,低位在后     *      * @param i     *            要转换的int参数     * @return 四个字节的byte数组     */    public static byte[] intToHlBytes4(int i)    {        byte[] result = new byte[4];        result[3] = (byte) (i & 0xFF);        System.out.println(result[3]);        result[2] = (byte) ((i >> 8) & 0xFF);        System.out.println(result[2]);        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;    }</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span>


转载请注明出处:http://blog.csdn.net/chenlinfeng772885775/article/details/50833849


0 0
原创粉丝点击