java中short,int转换成byte数组及byte数组转换成short,int

 private static byte[] shortToByteArray(short s) {
   byte[] shortBuf = new byte[2];
   for(int i=0;i<2;i++) {
   int offset = (shortBuf.length - 1 -i)*8;
   shortBuf[i] = (byte)((s>>>offset)&0xff);
   }
   return shortBuf;
  }
   public static final int byteArrayToShort(byte [] b) {
    return (b[0] << 8)
            + (b[1] & 0xFF);
   }
   public static byte[] intToByteArray(int value){
    byte[] b = new byte[4];
     for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
             b[i] = (byte) ((value >>> offset) & 0xFF);
      }
      return b;
   }
  public static final int byteArrayToInt(byte [] b) {
           return (b[0] << 24)
                   + ((b[1] & 0xFF) << 16)
                   + ((b[2] & 0xFF) << 8)
                   + (b[3] & 0xFF);
  }

  

原文地址:https://www.cnblogs.com/kaka1969/p/2317612.html