日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java java 大端_Java 大小端转换

發布時間:2023/12/4 java 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java java 大端_Java 大小端转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 java 大端_Java 大小端转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。