java byte處理

/**
     * 四字節(jié)轉int,適用于低位在前,高位在后
     * @param bytes
     * @param index
     * @return
     */
        public static int getInt_1(byte[] bytes,int index){
            int value=(int)((bytes[index]&0xff)
                    |(bytes[index+1]<<8&0xff00)
                    |(bytes[index+2]<<16&0xff0000)
                    |(bytes[index+3]<<24&0xff000000));
           return value;
        }

    /**
     * int 轉為長度為4的字節(jié)數(shù)組,適用于高位在后,低位在前
     * @param value
     * @return
     */
    public static byte[] getBytesFromInt_1(int value){
            byte[] temp=new byte[4];
            temp[3]=(byte) ((value&0xff000000)>>24);
            temp[2]=(byte) ((value&0xff0000)>>16);
            temp[1]=(byte) ((value&0xff00)>>8);
            temp[0]=(byte) ((value&0xff));
            return temp;
        }


    /**
     * 四字節(jié)轉int,適用于低位在后,高位在前
     * @param bytes
     * @param index
     * @return
     */
        public static int getInt_2(byte[] bytes,int index){
            int value=(int)((bytes[index]<<24&0xff000000)
                    |(bytes[index+1]<<16&0xff0000)
                    |(bytes[index+2]<<8&0xff00)
                    |(bytes[index+3]&0xff)
                    );
            return value;
        }

    /**
     * int 轉為長度為4的字節(jié)數(shù)組,適用于高位在前,低位在后
     * @param value
     * @return
     */
    public static byte[] getBytesFromInt_2(int value){
        byte[] temp=new byte[4];
        temp[0]=(byte) ((value&0xff000000)>>24);
        temp[1]=(byte) ((value&0xff0000)>>16);
        temp[2]=(byte) ((value&0xff00)>>8);
        temp[3]=(byte) ((value&0xff));
        return temp;
    }

    /**
     * 2字節(jié)數(shù)組轉換為short,適用于低位在前,高位在后
     * @param bytes
     * @param index
     * @return
     */
    public static short getShort_1(byte[] bytes,int index){
        return (short)(
                (bytes[index]&0xff)
                |(bytes[index+1]<<8&0xff00)
                );
    }

    /**
     * short轉長度為2的字節(jié)數(shù)組,低位在前,高位在后
     * @param value
     * @return
     */
    public static byte[]  getBytesFromShort_1(short value){
        byte[] temp=new byte[2];
        temp[0]=(byte) (value&0xff);
        temp[1]=(byte)(value>>8&0xff00);
        return temp;
    }
    /**
     * 2字節(jié)數(shù)組轉換為short,適用于低位在后,高位在前
     * @param bytes
     * @param index
     * @return
     */
    public static short getShort_2(byte[] bytes,int index){
        return (short)(
                (bytes[index+1]&0xff)
                |(bytes[index]<<8&0xff00)
                );
    }
    /**
     * short轉長度為2的字節(jié)數(shù)組,低位在后,高位在前
     * @param value
     * @return
     */
    public static byte[]  getBytesFromShort_2(short value){
        byte[] temp=new byte[2];
        temp[1]=(byte) (value&0xff);
        temp[0]=(byte)(value>>8&0xff00);
        return temp;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容