Java 大小端轉換

來源:互聯網
上載者:User

標籤:har   public   count   數組   unicode   int   for   byte   class   

package nlp.nlp;/** * 小端資料,Byte轉換 * */public class ByteConvert {    public static void main(String[] args) {        ByteConvert c = new ByteConvert();        c.Int2Bytes_LE(126);    }    public static final int UNICODE_LEN = 2;    /**     * int轉換為小端byte[](高位放在高地址中)     * @param iValue     * @return     */    public byte[] Int2Bytes_LE(int iValue){        byte[] rst = new byte[4];        // 先寫int的最後一個位元組        rst[0] = (byte)(iValue & 0xFF);        // int 倒數第二個位元組        rst[1] = (byte)((iValue & 0xFF00) >> 8 );        // int 倒數第三個位元組        rst[2] = (byte)((iValue & 0xFF0000) >> 16 );        // int 第一個位元組        rst[3] = (byte)((iValue & 0xFF000000) >> 24 );        return rst;    }    /**     * 轉換String為byte[]     * @param str     * @return     */    public byte[] String2Bytes_LE(String str) {        if(str == null){            return null;        }         char[] chars = str.toCharArray();         byte[] rst = Chars2Bytes_LE(chars);         return rst;    }    /**     * 轉換字元數組為定長byte[]     * @param chars              字元數組     * @return 若指定的定長不足返回null, 否則返回byte數組     */    public byte[] Chars2Bytes_LE(char[] chars){        if(chars == null)            return null;        int iCharCount = chars.length;                byte[] rst = new byte[iCharCount*UNICODE_LEN];        int i = 0;        for( i = 0; i < iCharCount; i++){            rst[i*2] = (byte)(chars[i] & 0xFF);            rst[i*2 + 1] = (byte)(( chars[i] & 0xFF00 ) >> 8);        }            return rst;    }    /**     * 轉換byte數組為int(小端)     * @return     * @note 數組長度至少為4,按小端方式轉換,即傳入的bytes是小端的,按這個規律組織成int     */    public int Bytes2Int_LE(byte[] bytes){        if(bytes.length < 4)            return -1;        int iRst = (bytes[0] & 0xFF);        iRst |= (bytes[1] & 0xFF) << 8;        iRst |= (bytes[2] & 0xFF) << 16;        iRst |= (bytes[3] & 0xFF)<< 24;        return iRst;    }    /**     * 轉換byte數組為int(大端)     * @return     * @note 數組長度至少為4,按小端方式轉換,即傳入的bytes是大端的,按這個規律組織成int     */    public int Bytes2Int_BE(byte[] bytes){        if(bytes.length < 4)            return -1;        int iRst = (bytes[0] << 24) & 0xFF;        iRst |= (bytes[1] << 16) & 0xFF;        iRst |= (bytes[2] << 8) & 0xFF;        iRst |= bytes[3] & 0xFF;        return iRst;    }    /**     * 轉換byte數組為Char(小端)     * @return     * @note 數組長度至少為2,按小端方式轉換     */    public char Bytes2Char_LE(byte[] bytes){        if(bytes.length < 2)            return (char)-1;        int iRst = (bytes[0] & 0xFF);        iRst |= (bytes[1] & 0xFF) << 8;                return (char)iRst;    }    /**     * 轉換byte數組為char(大端)     * @return     * @note 數組長度至少為2,按小端方式轉換     */    public char Bytes2Char_BE(byte[] bytes){        if(bytes.length < 2)            return (char)-1;        int iRst = (bytes[0] << 8) & 0xFF;        iRst |= bytes[1] & 0xFF;        return (char)iRst;    }}

  

Java 大小端轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.