最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java整數(shù)與byte數(shù)組的轉(zhuǎn)換實(shí)現(xiàn)代碼

 更新時(shí)間:2017年07月12日 15:27:49   作者:aotian16  
這篇文章主要介紹了java整數(shù)與byte數(shù)組的轉(zhuǎn)換實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

java整數(shù)與byte數(shù)組的轉(zhuǎn)換實(shí)現(xiàn)代碼

           這里對(duì)java中整數(shù)與byte數(shù)組的轉(zhuǎn)換進(jìn)行了實(shí)現(xiàn),平時(shí)的項(xiàng)目中很少用的到,但是特定需求的時(shí)候還是需要的,這里就記錄下,親測(cè)可用,

實(shí)現(xiàn)代碼:

public class NumberUtil { 
  /** 
   * int整數(shù)轉(zhuǎn)換為4字節(jié)的byte數(shù)組 
   * 
   * @param i 
   *      整數(shù) 
   * @return byte數(shù)組 
   */ 
  public static byte[] intToByte4(int i) { 
    byte[] targets = new byte[4]; 
    targets[3] = (byte) (i & 0xFF); 
    targets[2] = (byte) (i >> 8 & 0xFF); 
    targets[1] = (byte) (i >> 16 & 0xFF); 
    targets[0] = (byte) (i >> 24 & 0xFF); 
    return targets; 
  } 
 
  /** 
   * long整數(shù)轉(zhuǎn)換為8字節(jié)的byte數(shù)組 
   * 
   * @param lo 
   *      long整數(shù) 
   * @return byte數(shù)組 
   */ 
  public static byte[] longToByte8(long lo) { 
    byte[] targets = new byte[8]; 
    for (int i = 0; i < 8; i++) { 
      int offset = (targets.length - 1 - i) * 8; 
      targets[i] = (byte) ((lo >>> offset) & 0xFF); 
    } 
    return targets; 
  } 
 
  /** 
   * short整數(shù)轉(zhuǎn)換為2字節(jié)的byte數(shù)組 
   * 
   * @param s 
   *      short整數(shù) 
   * @return byte數(shù)組 
   */ 
  public static byte[] unsignedShortToByte2(int s) { 
    byte[] targets = new byte[2]; 
    targets[0] = (byte) (s >> 8 & 0xFF); 
    targets[1] = (byte) (s & 0xFF); 
    return targets; 
  } 
 
  /** 
   * byte數(shù)組轉(zhuǎn)換為無(wú)符號(hào)short整數(shù) 
   * 
   * @param bytes 
   *      byte數(shù)組 
   * @return short整數(shù) 
   */ 
  public static int byte2ToUnsignedShort(byte[] bytes) { 
    return byte2ToUnsignedShort(bytes, 0); 
  } 
 
  /** 
   * byte數(shù)組轉(zhuǎn)換為無(wú)符號(hào)short整數(shù) 
   * 
   * @param bytes 
   *      byte數(shù)組 
   * @param off 
   *      開始位置 
   * @return short整數(shù) 
   */ 
  public static int byte2ToUnsignedShort(byte[] bytes, int off) { 
    int high = bytes[off]; 
    int low = bytes[off + 1]; 
    return (high << 8 & 0xFF00) | (low & 0xFF); 
  } 
 
  /** 
   * byte數(shù)組轉(zhuǎn)換為int整數(shù) 
   * 
   * @param bytes 
   *      byte數(shù)組 
   * @param off 
   *      開始位置 
   * @return int整數(shù) 
   */ 
  public static int byte4ToInt(byte[] bytes, int off) { 
    int b0 = bytes[off] & 0xFF; 
    int b1 = bytes[off + 1] & 0xFF; 
    int b2 = bytes[off + 2] & 0xFF; 
    int b3 = bytes[off + 3] & 0xFF; 
    return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論

巴林左旗| 黄浦区| 太仆寺旗| 沂源县| 云浮市| 都昌县| 弥渡县| 张北县| 霍山县| 靖江市| 嘉荫县| 平舆县| 扬中市| 周口市| 法库县| 乌海市| 鄂州市| 乌兰察布市| 称多县| 万安县| 镇坪县| 乐平市| 安岳县| 介休市| 巫溪县| 甘南县| 老河口市| 乌苏市| 江门市| 沙雅县| 正蓝旗| 荣昌县| 大名县| 伊春市| 威海市| 迁安市| 东兰县| 中牟县| 富蕴县| 桐乡市| 昭觉县|